简体   繁体   English

JavaScript 正则表达式匹配

[英]JavaScript regular expression match

Consider the following:考虑以下:

var params = location.search.match(/=([\w\d-]+)&?/g);
console.log(params);

The output is: output 是:

["=7&", "=31500&", "=1"]

I don't wont any signs there, digits or words only, so I've set parentheses, but it doesn't work.我不会有任何符号,只有数字或单词,所以我设置了括号,但它不起作用。 So how do I do it?那么我该怎么做呢?

Are you getting the querystring parameter?你得到查询字符串参数了吗? I think this is what you want (although it doesn't use regular expression).我认为这就是您想要的(尽管它不使用正则表达式)。

<script type="text/javascript">
<!--
function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

var koko = querySt("koko");

document.write(koko);
document.write("<br>");
document.write(hu);
-->
</script>

Reference: http://ilovethecode.com/Javascript/Javascript-Tutorials-How_To-Easy/Get_Query_String_Using_Javascript.shtml参考: http://ilovethecode.com/Javascript/Javascript-Tutorials-How_To-Easy/Get_Query_String_Using_Javascript.shtml

There's a nice javascript function called gup() which makes this sort of thing simple.有一个很好的 javascript function 叫做gup() ,它使这种事情变得简单。 Here's the function:这是 function:

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

and sample usage:和示例用法:

var myVar = gup('myVar');

So, if your querystring looks like this: ?myVar=asdf因此,如果您的查询字符串如下所示: ?myVar=asdf

myVar will return 'asdf'. myVar 将返回“asdf”。

The .match method returns the whole matched string, not any groupings you have defined with parenthesis. .match方法返回整个匹配的字符串,而不是您用括号定义的任何分组。

If you want to return just a grouping in a regular expression, you'll have to use the .exec method multiple times, and extract the matched group from the resulting array:如果您只想返回正则表达式中的分组,则必须多次使用.exec方法,并从结果数组中提取匹配的组:

var search = location.search, 
    param = /=([\w\d-]+)&?/g, 
    params = [],
    match;
while ((match = param.exec(search)) != null) {
    params.push(match[1]);
}
console.log(params);

This works because the g flag is used on the regular expression.这是因为g标志用于正则表达式。 Every time you call .exec on the param regular expression, it's lastIndex attribute is set to the next matching substring and that in turn makes sure that the next call to .exec starts searching at the next match.每次在param正则表达式上调用.exec时,它的lastIndex属性都会设置为下一个匹配的 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ ,这反过来又确保对.exec的下一次调用在下一个匹配时开始搜索。 The resulting array contains the whole matched string at index 0, then every matched group at subsequent positions.结果数组包含索引 0 处的整个匹配字符串,然后是后续位置的每个匹配组。 Your group is thus returned as index 1 of the array.因此,您的组作为数组的索引 1 返回。

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

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