0

I have an href link as follows.i have to spilt name value and id value separately.

[email protected]&id=68

Using jquery how can i split [email protected] and 68 i tried the following one

var val =location.href.split('?')[1] which outputs the [email protected]&id=68

i have to output [email protected] and 68 separately..how it is possible..

Psl
  • 3,830
  • 16
  • 46
  • 84

4 Answers4

1
var str="[email protected]&id=68";

var emailrogh= str.split("&");

email=emailrogh[0];// show [email protected]

var idrough=emailrogh[1].split("=");

var id=idrough[1];//show 68

update

var str="[email protected]&id=68";

str=str.split("name=")[1];

var emailrogh= str.split("&");

email=emailrogh[0];// show [email protected]

var idrough=emailrogh[1].split("=");

var id=idrough[1];//show 68
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
1

may be:

var str = "[email protected]&id=68";
var splitted = str.replace("id=", "").split("&");
console.log( splitted );
//gives ["[email protected]", "68"]
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

use this function

function getParameterByName(name) 
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

this function will return your value directly.

for eg. for your link

[email protected]&id=68

use this function as

var email = getParameterByName("names");
var id = getParameterByName("id");

values would be

email = "[email protected]";
id = "68";
Sohil Desai
  • 2,940
  • 5
  • 23
  • 35
1
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Nirmal
  • 924
  • 4
  • 9