admin管理员组文章数量:1406911
thanks for looking at my question.
I have a long list of alternatives that I am trying to match in a regex:
var re = new RegExp('o1|o2|o3|o4|o5|...','g')
The problem that I run into is what happens if o1 is a substring of o2. For example
var re = new RegExp('a|b|c|ab|abc','g')
var s = 'abc'
s.match(re)
-> ["a", "b", "c"]
I would like for it to also be able to match the "ab" and "abc". I realize if I change the ordering of the RegExp, I can get it to match the longer string, but I really want to get all matches.
What is the best way to do this? This doesn't necessarily seem like the best (or a good way) of dealing with a long list of alternatives. I thought of testing each alternative with its own regexp, but that seemed less efficient.
Any guidance would be great. Thanks!
thanks for looking at my question.
I have a long list of alternatives that I am trying to match in a regex:
var re = new RegExp('o1|o2|o3|o4|o5|...','g')
The problem that I run into is what happens if o1 is a substring of o2. For example
var re = new RegExp('a|b|c|ab|abc','g')
var s = 'abc'
s.match(re)
-> ["a", "b", "c"]
I would like for it to also be able to match the "ab" and "abc". I realize if I change the ordering of the RegExp, I can get it to match the longer string, but I really want to get all matches.
What is the best way to do this? This doesn't necessarily seem like the best (or a good way) of dealing with a long list of alternatives. I thought of testing each alternative with its own regexp, but that seemed less efficient.
Any guidance would be great. Thanks!
Share Improve this question asked Oct 21, 2011 at 6:40 bobjenkins1234bobjenkins1234 431 gold badge1 silver badge3 bronze badges 3- So you want all possible matches of these alternatives? I don't think that's possible with only an expression. – Felix Kling Commented Oct 21, 2011 at 6:43
- Yes, that is what I want. I had used this solution in the past where I had a list of options with no overlap and it seemed to be faster than checking each option in its own regexp or using indexOf. I think because of the potential overlap in alternatives I just need to iterate over the list :-/ – bobjenkins1234 Commented Oct 21, 2011 at 6:48
- Google led me to this page when I was searching for alternatives to regex. Would like to share this very useful link that I eventually found: github./VerbalExpressions – Mikael Lindqvist Commented Mar 28, 2019 at 5:49
3 Answers
Reset to default 1If you have only the long list of alternatives in your RegExp the better way to do it is using the indexOf
method of String
. Here is the code which outputs indexes of all alternatives in the string:
var alternatives = ['a', 'b', 'c', 'ab', 'abc'],
s = 'abc, cba',
i,
index;
for (i = 0; i < alternatives.length; i++) {
index = -1;
do {
index = s.indexOf(alternatives[i], index+1);
if (index !== -1) {
console.log(alternatives[i], index);
}
} while (index !== -1);
}
If you try to match the whole string like "abc"
then the Rgex would be:
^(a|b|c|ab|abc)$
But there is maybe an easier way, but to help you, I have to know all "alternatives" you like to check for. Maybe a shorter regex expression is possible.
You could setup multiple (capturing groups)
to get all matches... You still need to order your alternatives accordingly
Using your example:
var re = /((a)(b))(c)|(a)(b)|a|b|c/
var s5 = 'abc';
var s4 = 'ab';
var s3 = 'a';
var s2 = 'b';
var s1 = 'c';
console.log(s5.match(re)); // ['abc', 'ab', 'a', 'b', 'c', undef, undef]
console.log(s4.match(re)); // ['ab', undef, undef, undef, undef, 'a', 'b']
console.log(s3.match(re)); // ['a', ... undef x 6 ...]
console.log(s2.match(re)); // ['b', ... undef x 6 ...]
console.log(s1.match(re)); // ['c', ... undef x 6 ...]
More info on capturing groups
本文标签: javascriptRegular Expression Alternatives (All Matches)Stack Overflow
版权声明:本文标题:javascript - Regular Expression Alternatives (All Matches) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744987391a2636173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论