Latest web development tutorials

Regular Expressions - operator precedence

Regular expressions are calculated from left to right, and follow the order of precedence, which is very similar to an arithmetic expression.

The same priority operation from left to right, the first operation of different priorities from high to low. The following table from highest to lowest priority order illustrates the various regular expression operators:

运算符 描述
\ 转义符
(), (?:), (?=), [] 圆括号和方括号
*, +, ?, {n}, {n,}, {n,m} 限定符
^, $, \任何元字符、任何字符 定位点和序列(即:位置和顺序)
| 替换,"或"操作
字符具有高于替换运算符的优先级,使得"m|food"匹配"m"或"food"。若要匹配"mood"或"food",请使用括号创建子表达式,从而产生"(m|f)ood"。