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?

Share Improve this question asked Aug 15, 2013 at 15:34 F. RakesF. Rakes 1,5373 gold badges17 silver badges25 bronze badges 4
  • 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
Add a ment  | 

4 Answers 4

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