正则表达式匹配以问号结尾的短语
我正在尝试找出一个 javascript 正则表达式,它将匹配以问号结尾但不包含在引号中的确切短语。到目前为止,我有这个,它与短语“somephrase”匹配,但我不知道如何匹配“somephrase?”。任何帮助将不胜感激。
(?<!"|')\some phrase\b(?!"|')
I'm trying to figure out a javascript regex that'll match an exact phrase that ends with a question mark, but isn't wrapped in quotes. So far I have this, which matches the phrase "some phrase", but I can't figure out how to match "some phrase?". Any help would be greatly appreciated.
(?<!"|')\some phrase\b(?!"|')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript 中不存在 Lookbehind。使用以下模式:
(?:[^"']|^)(某些短语\?)(?!["'])
。[^"']|^
表示:任何非引号字符或字符串的开头。示例:
短语周围的括号标记可引用的组。
(?:
表示:创建一个组,但取消引用它。要引用它,请参阅示例代码。因为 JavaScript 中不存在后向查找,所以不可能创建一个检查前缀是否存在的模式不存在。Lookbehinds don't exist in JavaScript. Use the following pattern:
(?:[^"']|^)(some phrase\?)(?!["'])
.[^"']|^
means: any non-quote character or the beginning of a string.Example:
The parentheses around the phrase mark a referable group.
(?:
means: Create a group, but dereference it. To refer back to it, see the example code. Because lookbehinds don't exist in JavaScript, it's not possible to create a pattern which checks whether a prefix does not exist.试试这个:
http://jsfiddle.net/gilly3/zCUsg/
Try this:
http://jsfiddle.net/gilly3/zCUsg/