Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the second last parameter in a url using javascript

Tags:

javascript

I have a url like this

http://example.com/param1/param2/param3

Please help me get the second last parameter using javascript. I searched and could only find regex method to find the last parameter. I am new to this. Any help would be greatly appreciated.

Thanks.

like image 924
GBRocks Avatar asked Nov 30 '22 01:11

GBRocks


1 Answers

var url = 'http://example.com/param1/param2/param3';
var result= url.split('/');
var Param = result[result.length-2];

Demo Fiddle:http://jsfiddle.net/HApnB/

Split() - Splits the string into an array of strings based on the separator you mentioned

In the above , result will be an array that contains

result = [http:,,example.com,param1,param2,param3];
like image 171
Prasath K Avatar answered Dec 22 '22 04:12

Prasath K