javascript regex - 寻找替代方案? [英] javascript regex - look behind alternative?

查看:39
本文介绍了javascript regex - 寻找替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个在大多数正则表达式实现中都能正常工作的正则表达式:

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 不匹配

可以在 Regular 找到此正则表达式的正确解释表达式匹配不包含单词的字符串?

javascript 1.5 版 起就可以使用展望,并且所有主要浏览器都支持

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 regex - 寻找替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆