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)。

相关文章:

  • 2021-11-15
  • 2021-08-15
  • 2021-04-17
  • 2022-01-06
  • 2021-11-11
猜你喜欢
  • 2021-06-10
  • 2022-12-23
  • 2021-05-17
  • 2022-01-08
  • 2021-07-04
  • 2021-12-24
  • 2021-11-04
相关资源
相似解决方案