1

我正在尝试编写一个正则表达式,我需要你的帮助......

要求- 第一个字符是“ s ”,那么后面必须跟两个数字 Eg - S98 如果不是“ s ”,那么后面必须跟两个字母字符{AZ, az},例如 -aIO

我像以前一样写过,但它不起作用。如果我分别分成两部分(在|之前和之后),它是有效的,但不是在一起..

regexevent = /^([s]{1})([0-9]{2})| ([a-rt-z]{1})([A-Za-z]{2})$/;

请帮忙

4

1 回答 1

4

The $ and ^ are part of the OR, so it's looking for the LHS start anchored or RHS end anchored. You need to wrap the whole thing in parenthesis...

regexevent = /^(([s]{1})([0-9]{2})| ([a-rt-z]{1})([A-Za-z]{2}))$/;

Alternatively, you could write your regex a bit terser...

regexevent = /^(s\d{2}|[a-rt-z][A-Za-z]{2})$/;

(Assuming some of those capturing groups aren't required.)

Also...

first character is "s", then it must be followed by two numeric digits Eg - S98

Your regex will fail if it's a S (you only check for s). You could use [sS].

于 2013-07-02T10:22:24.473 回答