Javascript:负面的lookbehind相当于? [英] Javascript: negative lookbehind equivalent?

查看:147
本文介绍了Javascript:负面的lookbehind相当于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在javascript中实现相当于负面反馈常用表达?我需要匹配一个不以特定字符集开头的字符串。

Is there a way to achieve the equivalent of a negative lookbehind in javascript regular expressions? I need to match a string that does not start with a specific set of characters.

似乎我无法找到一个正则表达式,如果匹配的部分不会失败在字符串的开头找到。消极的外观似乎是唯一的答案,但javascript没有。

It seems I am unable to find a regex that does this without failing if the matched part is found at the beginning of the string. Negative lookbehinds seem to be the only answer, but javascript doesn't have one.

编辑:
这是我想要工作的正则表达式,但是它没有:

This is the regex that I would like to work, but it doesn't:

(?<!([abcdefg]))m

所以它会匹配'jim'或'm'中的'm',但不会匹配'jam'

So it would match the 'm' in 'jim' or 'm', but not 'jam'

推荐答案

Lookbehind断言得到接受进入 ECMAScript规范,已在 V8 和< a href =https://bugs.chromium.org/p/v8/issues/detail?id=4545\"rel =noreferrer>在没有Google Chrome v62的标志和 Node.js v6 g和v9没有标志。因此,如果您正在开发仅限Chrome的环境(例如 Electron ),或节点,你今天就可以开始使用lookbehinds了!

Lookbehind Assertions got accepted into the ECMAScript specification in 2018. This has been implemented in V8 and shipped without flags with Google Chrome v62 and in Node.js v6 behind a flag and v9 without a flag. So, if you're developing for a Chrome-only environment (such as Electron), or Node, you can start using lookbehinds today!

正面看后用法:

Positive lookbehind usage:

console.log(
  "$9.99  €8.47".match(/(?<=\$)\d+(\.\d*)?/) // Matches "9.99"
);

负面的背后使用:

Negative lookbehind usage:

console.log(
  "$9.99  €8.47".match(/(?<!\$)\d+(?:\.\d*)/) // Matches "8.47"
);

支持其他平台:


  • Mozilla Firefox正在研究它:跟踪这里

  • Microsoft Edge也正在研究它:跟踪此处(用户声音建议)。

  • Mozilla Firefox is working on it: Tracked here.
  • Microsoft Edge is working on it too: Tracked here (user voice suggestion).

这篇关于Javascript:负面的lookbehind相当于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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