javascript正则表达式 - 看看替代方案? [英] javascript regex - look behind alternative?
问题描述
这是一个在大多数正则表达式实现中都能正常工作的正则表达式:
Here is a regex that works fine in most regex implementations:
(?<!filename)\.js$
这匹配.js为一个以.js结尾的字符串,除了filename.js
This matches .js for a string which ends with .js except for filename.js
Javascript没有正则表达式。有没有人能够组合一个替代正则表达式,它可以实现相同的结果并在javascript中工作?
Javascript doesn't have regex lookbehind. Is anyone able put together an alternative regex which achieve the same result and works in javascript?
这里有一些想法,但需要帮助函数。我希望只用一个正则表达式实现它:
http:// blog。 stevenlevithan.com/archives/mimic-lookbehind-javascript
Here are some thoughts, but needs helper functions. I was hoping to achieve it just with a regex: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
推荐答案
^(?! filename)。+ \。js
适合我
测试:
- test.js匹配
- blabla.js匹配
- filename.js无匹配
这个正则表达式的正确解释可以在正则表达式匹配不包含单词的字符串?
A proper explanation for this regex can be found at Regular expression to match string not containing a word?
从 1.5版的javascript 并得到所有主流浏览器的支持
Look ahead is available since version 1.5 of javascript and is supported by all major browsers
更新了以匹配filename2.js和2filename.js但不是filename.js
Updated to match filename2.js and 2filename.js but not filename.js
(^(?!filename\.js $)。)。+ \。js
这篇关于javascript正则表达式 - 看看替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!