Javascript 如何匹配但排除正则表达式模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3003310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I match on, but exclude a regex pattern?
提问by Adam
I have this URL:
我有这个网址:
http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&
http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&
And this regex pattern:
这个正则表达式模式:
cID=[^&]*
Which produces this result:
产生这个结果:
cID=87B6XYZ964D293CF
How do I REMOVE the "cID="?
如何删除“cID=”?
Thanks
谢谢
回答by Kerry Jones
You can either use lookbehind (not in Javascript):
您可以使用lookbehind(不在Javascript中):
(?<=cID=)[^&]*
Or you can use grouping and grab the first group:
或者您可以使用分组并获取第一组:
cID=([^&]*)
回答by polygenelubricants
Generally speaking, to accomplish something like this, you have at least 3 options:
一般来说,要完成这样的事情,您至少有 3 个选择:
- Use lookarounds, so you can match precisely what you want to capture
- No lookbehind in Javascript, unfortunately
- Use capturing group to capture specific strings
- Near universally supported in all flavors
- If all else fails, you can always just take a
substringof the match- Works well if the length of the prefix/suffix to chop is a known constant
- 使用环视,以便您可以精确匹配要捕获的内容
- 不幸的是,Javascript 中没有回溯
- 使用捕获组捕获特定字符串
- 几乎所有口味都支持
- 如果所有其他方法都失败了,您可以随时参加
substring比赛- 如果要切割的前缀/后缀的长度是已知常量,则效果很好
References
参考
Examples
例子
Given this test string:
鉴于此测试字符串:
i have 35 dogs, 16 cats and 10 elephants
These are the matches of some regex patterns:
这些是一些正则表达式模式的匹配:
\d+ cats->16 cats(see on rubular.com)\d+(?= cats)->16(see on rubular.com)(\d+) cats->16 cats(see on rubular.com)- Group 1 captures
16
- Group 1 captures
\d+ cats->16 cats(见rubular.com)\d+(?= cats)->16(见rubular.com)(\d+) cats->16 cats(见rubular.com)- 第 1 组捕获
16
- 第 1 组捕获
You can also do multiple captures, for example:
您还可以进行多次捕获,例如:
(\d+) (cats|dogs)yields 2 match results (see on rubular.com)- Result 1:
35 dogs- Group 1 captures
35 - Group 2 captures
dogs
- Group 1 captures
- Result 2:
16 cats- Group 1 captures
16 - Group 2 captures
cats
- Group 1 captures
- Result 1:
(\d+) (cats|dogs)产生 2 个匹配结果(参见 rubular.com)- 结果 1:
35 dogs- 第 1 组捕获
35 - 第 2 组捕获
dogs
- 第 1 组捕获
- 结果 2:
16 cats- 第 1 组捕获
16 - 第 2 组捕获
cats
- 第 1 组捕获
- 结果 1:
回答by gnarf
With JavaScript, you'll want to use a capture group(put the part you want to capture inside ()) in your regular expression
使用 JavaScript,您需要在正则表达式中使用捕获组(将要捕获的部分放在里面())
var url = 'http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&';
var match = url.match(/cID=([^&]*)/);
// ["cID=876XYZ964D293CF", "876XYZ964D293CF"]
// match[0] is the whole pattern
// match[1] is the first capture group - ([^&]*)
// match will be 'false' if the match failed entirely
回答by cherouvim
By using capturing groups:
通过使用捕获组:
cID=([^&]*)
and then get $1:
然后得到 1 美元:
87B6XYZ964D293CF
回答by David A Moss
Here's the Javascript code:
这是Javascript代码:
var str = "http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&";
var myReg = new RegExp("cID=([^&]*)", "i");
var myMatch = myReg.exec(str);
alert(myMatch[1]);
回答by tylik
There is a special syntax in javascript which allows you to exclude unwanted match from the result. The syntax is "?:" In your case the solution would be the following
javascript 中有一种特殊的语法,它允许您从结果中排除不需要的匹配。语法是“?:”在您的情况下,解决方案如下
'http://example.com/createSend/step4_1.aspx?cID=876XYZ964D293CF&snap=true&jlkj=kjhkjh&'.match(/(?:cID=+)([^&]*)/)[1];

