admin管理员组文章数量:1291367
I'm a bit rusty on my regexp and Javascript. I have the following string var:
var subject = "/admin.php?page=settings&tabs_added[114787535263592]=1&tabs_added[217770811582323]=1&tabs_added[198738186831542]=1"
I want to extract 114787535263592
, 217770811582323
and 198738186831542
.
I've tried to use non-capturing parenthesis (?:)
:
var regexp = /(?:tabs_added[\[])(\d)+(?:[\]])/;
var pageid = regexp.exec(subject);
But the result I get (["tabs_added[114787535263592]", "2"]
) is not what I expected -- what am I doing wrong? Here's a jsFiddle: /
I'm a bit rusty on my regexp and Javascript. I have the following string var:
var subject = "/admin.php?page=settings&tabs_added[114787535263592]=1&tabs_added[217770811582323]=1&tabs_added[198738186831542]=1"
I want to extract 114787535263592
, 217770811582323
and 198738186831542
.
I've tried to use non-capturing parenthesis (?:)
:
var regexp = /(?:tabs_added[\[])(\d)+(?:[\]])/;
var pageid = regexp.exec(subject);
But the result I get (["tabs_added[114787535263592]", "2"]
) is not what I expected -- what am I doing wrong? Here's a jsFiddle: http://jsfiddle/KgpAw/
-
Your
(?:)
group looks fine, but the(?+)
looks fishy. – Pointy Commented Mar 29, 2012 at 14:35 -
@andrewcooke Sorry about that, I messed up when pasting. It's a
?:
. – julien_c Commented Mar 29, 2012 at 14:35 - @T.J.Crowder You're totally right. I added a link to jsFiddle that shows what the result is. – julien_c Commented Mar 29, 2012 at 14:38
4 Answers
Reset to default 5You were very close. You need to capture the +
as well. Otherwise you only capture one decimal digit:
var regexp = /(?:tabs_added[\[])(\
You should also make your regex global to find all matches:
var regexp = /(?:tabs_added[\[])(\d+)(?:[\]])/g;
Then you should loop through the results, you also don't need to have non-capturing groups because you're not using a quantifier on them (?
, +
, or *
), nor do you need to put your [
and ]
inside a character class:
var regexp = /tabs_added\[(\d+)\]/g;
var result;
while(null != (result = regexp.exec(subject))){
document.write(result[1] + '<br />');
}
JSFiddle Example
Note: It's better to use console.log
for debugging purposes than document.write
as long as you have a console available. (Chrome, Opera, IE9, and Firefox have consoles built in, and I think Safari does too, so you should have one available.) console.log
provides much more valuable information when logging objects and arrays.
You need global flag and match all digits: (\d+)
instead of (\d)+
.
And don't need all those non-capturing subgroups.
var regexp = /tabs_added\[(\d+)\]/g;
var pageIds = [], match;
while(match = re.exec(str)) pageIds.push(match[1]);
pageIds;
Hi i can solve above requirement in one line code.
<!DOCTYPE html>
<html>
<body>
<script>
var str="/admin.php?page=settings&tabs_added[114787535263592]=1&tabs_added[217770811582323]=1&tabs_added[198738186831542]=1";
var patt1=/\d{15}/g;
document.write(str.match(patt1));
</script>
</body>
</html>
For multiple matches, you need to use the g modifier. Please try the following
var regexp = /(?:tabs_added[\[])(\d)+(?:[\]])/g;
var pageid = subject.match(regexp);
This will still contain the tabs_added part, but that can be removed separately
本文标签: Javascript Regex and noncapturing parenthesesStack Overflow
版权声明:本文标题:Javascript Regex and non-capturing parentheses - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741531405a2383777.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论