欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS 正则表达式的位置匹配

程序员文章站 2022-03-29 20:41:30
上面这个网站可以用于在线检测js的正则表达式语法 除了熟知的几个固定字符表示位置: ^ : match the beginning of the st...

上面这个网站可以用于在线检测js的正则表达式语法

除了熟知的几个固定字符表示位置:

^ : match the beginning of the string and, in multiline searches, the beginning of a line.

$ : match the end of the string and, in multiline searches, the end of a line.

\b: 

match a word boundary. that is, match the position between a \w character and a \w character or between a \w character and the beginning or end of a string. (note, however, that [\b] matches backspace.)

\b: match a position that is not a word boundary.

还有的就是使用正则表达式来确定要匹配的位置,也叫做zero-width test(零宽断言)

(?=p) :

a positive lookahead assertion. require that the following characters match the pattern p, but do not include those characters in the match.

(?!p) :

a negative lookahead assertion. require that the following characters do not match the pattern p.

对于(?=p)和(?!p)的使用举一个例子:

要在url(skins/default/images/index/default.png)中匹配"/default/"中的"default",而不匹配"/default.png"中的"default"?

正则表达式: (?!\/)default(?=\/)

其中(?!\/)表示以"/"开头,(?=\/)表示以"/"结尾