问题描述
比方说,我有一个字符串"QQxaxbxcQQ",我想捕获所有x组,后跟任意字符.我也只想在QQ之间搜索(该字符串可能包含其他内容).我以为这会行得通的:
Let's say I had a string "QQxaxbxcQQ", and I wanted to capture all groups of x followed by any character. I also only want to search between the QQ's (the string may include other things in it). I assumed this would have worked:
var matches = str.match(/QQ(x\w)+QQ/)
但是,这似乎只给我返回了最后一场比赛(xc).你能指出我正确的方向吗?
However, this only seems to return to me the last match (xc). Can you point me in the right direction?
我的问题的第一个版本过于简化.向原始响应者致歉.进行了修改,使其更接近我的实际问题.
the first version of my question was oversimplified. Apologies to original responders. Edited to make it closer to my actual problem.
推荐答案
您只需要在正则表达式的末尾添加g
参数. g
标志返回一个包含所有匹配项的数组,在本例中,该数组包含向后引用(x\w)
You only need to add g
parameter at end of your regex. g
flag returns an array containing all matches, in our case all matches of backreference (x\w)
此处以粗体显示:/QQ(x \ w)+ QQ/ g
In bold here: /QQ(x\w)+QQ/g
var matches = str.match(/QQ(x\w)+QQ/g)
matches是一个数组
matches is an array
看看这个: http://jsfiddle.net/SZRSA/3/
这篇关于如何量化Javascript的正则表达式中的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!