admin管理员组文章数量:1406937
Using javascript and .match()
can I simplify this regex?
I want to match against a alphanumeric code of 5x5 or 3x5 blocks that is seperated by hyphens.
For example I want to match:
123EF-12B45-123H5-A2CGE-A2345
or
54321-ABCDE-F2345
So far I tried (\w{5}-){4}\w{5}
to match the first example and (\w{5}-){2}\w{5}
to match the second. Is there a way to match either or? I tried bining the with |
but it only matched the 3 pairs.
Or is it better to run regex .match()
twice for each pattern?
Using javascript and .match()
can I simplify this regex?
I want to match against a alphanumeric code of 5x5 or 3x5 blocks that is seperated by hyphens.
For example I want to match:
123EF-12B45-123H5-A2CGE-A2345
or
54321-ABCDE-F2345
So far I tried (\w{5}-){4}\w{5}
to match the first example and (\w{5}-){2}\w{5}
to match the second. Is there a way to match either or? I tried bining the with |
but it only matched the 3 pairs.
Or is it better to run regex .match()
twice for each pattern?
- Can you show us how exactly you tried to bine them? – Bergi Commented Aug 15, 2013 at 15:41
- use two separate regular expressions. The code will be more readable and easier to maintain. Stop trying to cram too much into one regular expression that is impossible to understand. – zzzzBov Commented Aug 15, 2013 at 15:43
- I just tried this in a Javascript console and bining them seems to work okay for me. Are you sure you're bining the regexes correctly? Can you post the exact code you are using? – c.maclean Commented Aug 15, 2013 at 15:49
- I matched against the smaller pairs first, so it couldn't catch the bigger one. – F. Rakes Commented Aug 15, 2013 at 15:56
4 Answers
Reset to default 2(\w{5}-){2}\w{5}|(\w{5}-){4}\w{5}
The alternation |
will first try to match the left subexpression, and when that fails it tries the right one. When the left one includes the right one, it will never match the right one. In your case it will stop at only three groups even if there are more. Try to simply exchange them:
(\w{5}-){4}\w{5}|(\w{5}-){2}\w{5}
You also could bine them into one:
((\w{5}-){4}|(\w{5}-){2})\w{5}
(\w{5}-){2}((\w{5}-){2})?\w{5}
You could use
^(\w{5}-){2}\w{5}((-\w{5}){2})?$
(\w{5}-){2}\w{5}
matches the 3x5 case and optionally there can be two more blocks, which would be the 5x5 case.
Alternatively you could use
^(\w{5}-){2}((\w{5}-){2})?\w{5}$
i.e. put the optional block in between, or a simple bination of your expressions:
^((\w{5}-){4}\w{5})|(\w{5}-){2}\w{5})$
You could try specified range for blocks with hypen:
(\w{5}-){2,4}\w{5}
And this (unfortunately or not) also will match 4x5 block, i.e.
54321-ABCDE-F2345-ABF45
This way worked for me:
"123EF-12B45-123H5-A2CGE-A2345".match(/(\w{5}-){4}\w{5}|(\w{5}-){2}\w{5}/)
=> ["123EF-12B45-123H5-A2CGE-A2345", "A2CGE-", undefined]
"123EF-12B45-123H5".match(/(\w{5}-){4}\w{5}|(\w{5}-){2}\w{5}/)
=> ["123EF-12B45-123H5", undefined, "12B45-"]
本文标签: Regex javascriptmatch either orStack Overflow
版权声明:本文标题:Regex javascript - match either or - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744972910a2635342.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论