当前位置: 首页 > 图文教程 > 网络编程 > 正则表达式 > JS 正则表达式的位置匹配

正则表达式
DreamWeaver中使用正则技术搜索
代替正则——HyperScriptExpression联合开发倡议公告
正则入门连载!(献给不及格的程序员们)
请教一个正则表达式,匹配所有Html标签外部的指定字符串
php正则表达式中的非贪婪模式匹配
js中2005-05-02怎么转换为2005/5/2?
用正则表达式格式化html标签的代码
php利用正则表达式取出图片的URL
用正则取出html页面中script段落里的内容
学习正则表达式30分钟入门教程(第二版)
只能是字母或数字或者是字母和数字的组合的正则previousSibling
[php]正则表达式的五个成功习惯
常用正则表达式语法例句
正则表达式基础教程 regular expression
php中正则表达式中的特殊符号
PHP和正则表达式教程集合之一
PHP和正则表达式教程集合之二
用正则实现提取代码内容的代码
php正则之函数 preg_replace()参数说明
关于preg_replace函数的问题讲解

JS 正则表达式的位置匹配


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 80 ::
收藏到网摘: n/a

JS中正则表达式的位置匹配代码,国外翻译的文章。

http://regexpal.com/

上面这个网站可以用于在线检测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(?=\/)

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