Regex javascript - match either or -
using javascript , .match()
can simplify regex? want match against alphanumeric code of 5x5 or 3x5 blocks seperated hyphens.
for example want match:
123ef-12b45-123h5-a2cge-a2345
or
54321-abcde-f2345
so far tried (\w{5}-){4}\w{5}
match first example , (\w{5}-){2}\w{5}
match second. there way match either or? tried combining |
matched 3 pairs.
or better run regex .match()
twice each pattern?
(\w{5}-){2}\w{5}|(\w{5}-){4}\w{5}
the alternation |
first try match left subexpression, , when fails tries right one. when left 1 includes right one, never match right one. in case stop @ 3 groups if there more. try exchange them:
(\w{5}-){4}\w{5}|(\w{5}-){2}\w{5}
you combine them one:
((\w{5}-){4}|(\w{5}-){2})\w{5} (\w{5}-){2}((\w{5}-){2})?\w{5}
Comments
Post a Comment