简体   繁体   English

需要Javascript正则表达式帮助

[英]Javascript regular expression help needed

I have a string: 我有一个字符串:

User_TimeStamp=20120712201112&email_id=ufe.test20@markit.com&resetCode=**uNcoHR9O3wAAB46xw**&reset=true

I tried and Googled a lot to get the highlighted text. 我尝试并用Google搜索了很多内容以突出显示文本。 Basically I want a JavaScript regex to get all characters in between resetCode= and &reset=true . 基本上,我希望JavaScript正则表达式获取resetCode=&reset=true之间的所有字符。

Any help regarding this is highly appreciated. 对此,我们将给予任何帮助。

Thanks 谢谢

Two ways: 两种方式:

  1. Your regex should be resetCode=([^&]*) 您的正则表达式应为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. &上拆分字符串,然后遍历每个字符串,并在= resetCode第一个字符串与resetCode进行比较,并在找到后返回第二个字符串。

Try this code where you're looking for the resetCode variable 在寻找resetCode变量的地方尝试此代码

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

You can do something more 'readable': 您可以做一些更“可读”的事情:

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

jsBin demo jsBin演示

why not just use this handy function to get the value of resetCode 为什么不仅仅使用这个方便的函数来获取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"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM