1

I am having a case where i need to split given string using starts with regex (/^'searchString'/) which is not working such as

"token=123412acascasdaASDFADS".split('token=')

Here i want to extract the token value but as there might be some other possible parameters such as "reset_token=SDFDFdsf12313ADADF".split('token=')

Here it also split the string with 'token=', Thats why i need to split the string using some regex which states that split the string where it starts with given string.

Thanks..

EDITED Guys thanks for your valuable response this issue can be resolve using /\btoken=/ BUT BUT its does not work if 'token=' stored as a string into a variable such as

sParam = 'token=';
"token=123412acascasdaASDFADS".split(/\bsParam/);

This does not works.

CrazyGeek
  • 3,397
  • 2
  • 24
  • 38

5 Answers5

1

You can use regex in split with word boundary:

"token=123412acascasdaASDFADS".split(/\btoken=/)

If token is stored in a variable then use RegExp constructor:

var sParam = "token";
var re = new RegExp("\\b" + sParam + "=");

Then use it:

var tokens = "token=123412acascasdaASDFADS".split( re );
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for your answer but 'token=' is stored as a string into a variable name as sParam, now if i try "token=123412acascasdaASDFADS".split(/\bsParam/) than it does not work. – CrazyGeek Aug 19 '14 at 07:51
  • check my updated answer for using it from a variable. – anubhava Aug 19 '14 at 08:01
1

This is the use case for the \b anchor:

\btoken=

It ensures there's no other word character before token (a word character being [a-zA-Z0-9_])

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
1

You can try with String#match() function and get the matched group from index 1

sample code

var re = /^token=(.*)$/; 
var str = 'token=123412acascasdaASDFADS';


console.log('token=123412acascasdaASDFADS'.match('/^token=(.*)$/')[1]);

output:

123412acascasdaASDFADS

If token is dynamic then use RegExp

var token='token=';
var re = new RegExp("^"+token+"(.*)$");

var str = 'token=123412acascasdaASDFADS';

console.log(str.match(re)[1]);

Learn more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
1

You need to split the string using the & parameter delimiter, then loop through those parameters:

var token;
$.each(params.split('&'), function() {
    var parval = this.split('=');
    if (parval[0] == "token") {
        token = parval[1];
        return false; // end the $.each loop
    }
});

if you just use token= as the split delimiter, you'll include all the other parameters after it in the value.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

It's not clear what you need, but this may be an idea to work with?

var reqstr = "token=12345&reset_token=SDFDFdsf12313ADADF&someval=foo"
              .split(/[&=]/)
   ,req = [];
reqstr.map( function (v, i) {
             if (i%2==0) {
              var o = {}; 
              o[/token/i.test(v) ? 'token' : v] = reqstr[i+1]; 
              this.push(o);
             } return v
            }, req);
 /* => req now contains:
     [ { token: '12345' },
       { token: 'SDFDFdsf12313ADADF' },
       { someval: 'foo' } ]
 */
KooiInc
  • 119,216
  • 31
  • 141
  • 177