pranay vadel
pranay vadel

Reputation: 82

Javascript regular expression help needed

I have a string:

User_TimeStamp=20120712201112&[email protected]&resetCode=**uNcoHR9O3wAAB46xw**&reset=true

I tried and Googled a lot to get the highlighted text. Basically I want a JavaScript regex to get all characters in between resetCode= and &reset=true.

Any help regarding this is highly appreciated.

Thanks

Upvotes: 0

Views: 62

Answers (4)

Roko C. Buljan
Roko C. Buljan

Reputation: 206121

You can do something more 'readable':

retrieveDesired = User_TimeStamp.split('=')[3].split('&')[0];

jsBin demo

Upvotes: 1

jcolebrand
jcolebrand

Reputation: 16025

Try this code where you're looking for the resetCode variable

var resetCodeValue;
var s = window.location.split('&');
for(var k in s) { var pair = s[k].split('=');
   if (pair[0] == 'resetCode'){
      resetCodeValue = pair[1];
   }
}

Upvotes: 1

Ben
Ben

Reputation: 673

Two ways:

  1. Your regex should be resetCode=([^&]*)
  2. Split the string on & then iterate through each string and split on = comparing the first string to resetCode and return the second string when you find it.

Upvotes: 1

user1496176
user1496176

Reputation:

why not just use this handy function to get the value of resetCode

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
document.write(getUrlVars()["resetCode"]);

Upvotes: 0

Related Questions