js中定义正则表达式有两种,使用RegExp和使用字面量。
使用字面量定义时需要注意:必须以/开始,以/结束,就像定义字符串一样("test")。
但是,js的正则表达式可以通过指定flag控制匹配的规则,如下所示:
g
global match
i
ignore case
m
multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)
u
unicode; treat pattern as a sequence of unicode code points
y
sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes).
如果指定匹配规则的话,使用字面量定义正则表达式满足:
1.以/开始
2.最后一个/之后必须是g、i、m、u、y中的一个(可以同时指定多个flag)。