Two different regular expressions in one?
I want to put this regexs into one regex, but dont now how:
/^([0-9]{2,4})开发者_JS百科\-([0-9]{6,7})$/
/^\*([0-9]{4})$/
so the string can get XX(XX)-XXXXXX(X) or *XXXX
Thanks.
to merge two regular expressions A
and B
, do:
/(A|B)/
This may change the order of one set of capturing parentheses. Also note that parentheses may not be needed, e.g. A|B
may work in your simple case.
In other languages which support regular expressions with named captures, you can use those.
You can OR-them this:
XX(XX)-XXXXXX(X)|*XXXX
so that either will match...
the trouble you get is that when the second one matches you don't get \1 ($1) and \2 ($2) set...
Use an | (or).
i.e.:
/^([0-9]{2,4})\-([0-9]{6,7})|(\*([0-9]{4})$/
精彩评论