The regular master asks for advice, a problem of limiting the overall length.

regular: / ^ [u4e00-u9fa5] + (- [u4e00-u9fa5] +) * $/

description: for matching Chinese names found on the Internet, ethnic minorities are separated by horizontal lines. I now want to limit the overall length to 2-6 digits for advice. Thank you in advance


/ ^ (? =. {2 u4e00-u9fa5 6} $) [u4e00-u9fa5] + (- [u4e00-u9fa5] +) * $/ try


/(^[\u4e00-\u9fe5]{2,2}(-[\u4e00-\u9fe5]{2,4})*$)|(^[\u4e00-\u9fe5]{3,3}(-[\u4e00-\u9fe5]{2,3})*$)/
The

| operator matches the preceding subexpression or the subsequent subexpression.
because-there may be 2 or 3 words in front of it, if you limit the overall number of words, the number of matching words needs to change according to the previous, so there are two cases, use the | operator to select.

Menu