A problem of regular expressions

/ ^ ([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) | ([a-zA-Z]) $/ .test ("11a") / / true

and

/ ^ (([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) | ([a-zA-Z])) $/ .test ("11a") / / false

Why are the results different

The difference between the two regular expressions is that I added a parenthesis to the outermost layer


regular matching results are different. There is no reason why. You said that you added parentheses, you should understand the meaning of parentheses: group the matching results


Let's first explain the meaning of regularity.
decompose
/ ^ ([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) | ([a-zA-Z]) $/
^ ([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) | ([a-zA-Z])
enter a string The starting match of ([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) or ([a-zA-Z]) one of the conditions
([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) | ([a-zA-Z]) $
the end position of the input string matches fully ([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) or ([a-zA-Z]) one of the conditions
be more specific
[a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9] match for a-zA-Z once | match for [A-Za-z0-9] 0 times | | multiple times
[a-zA-Z] this means a-zA-z doesn't explain much
() match the result and get it

if you add () matching result to it, then the result of or (which can be understood as "or") is left regular [a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9] before the matching result is true !

I know I'm late, but can I just explain it to you?


begins with ^ and ends with $ has higher priority than | , so the first example is actually | two separate segments ^ ([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) and ([a-zA-Z]) $.

you can write down this priority

\                              
(), (?:), (?=), []              
*, +, ?, {n}, {n,}, {n,m}      
^, $, \    :
|                              ""

Let me join in the fun:

Let's first take a look at
/ ^ ([a-zA-Z] + [A-Za-z0-9 -] * [a-zA-Z0-9]) | ([a-zA-Z]) $/ the meaning of the regular expression
below is the regular railway diagram
clipboard.png

group -sharp1group -sharp2

"11a" group -sharp2


/^(([a-zA-Z]+[A-Za-z0-9-]*[a-zA-Z0-9])|([a-zA-Z]))$/

clipboard.png

group -sharp1group -sharp1

group -sharp1

group -sharp2 group -sharp3 group -sharp2 group -sharp3

group -sharp2 "[1-] ||[0-] |[1]"
group -sharp3 "[1]"

group -sharp2 "11a" group2 , group -sharp3a
clipboard.png

.

but group-sharp3 only hits the last trailing character, not the first character 1, so the whole match is not matched.

I don't know if this answer is clear enough.


Let me give a simple and rude explanation.
the first indicates that it begins with (group1) or ends with (group2), and the second represents


that begins with (group1) or begins with (group2).

Please take a look at the following case:

D:\temp>node test.js
1  true true
2  true true
12 true false
10 true false
02 true false

you can see that reg1 represents the entire string starts with 1 or ends with 2 , and reg2 indicates that the entire string can only be 1 or 2. It can be understood as | has lower priority than $ and ^ .

Menu