简体   繁体   English

Javascript中的特定正则表达式正面看起来(前后)

[英]Specific regex positive look(around|ahead|behind) in Javascript

I'm looking to match /(?=\\W)(gimme)(?=\\W)/gi or alike. 我想匹配/(?=\\W)(gimme)(?=\\W)/gi或类似的。 The \\W are supposed to be zero-width characters to surround my actual match. \\W应该是零宽度字符以包围我的实际匹配。

Maybe some background. 也许有些背景。 I want te replace certain words (always \\w+ ) with some literal padding added, but only if it's not surrounded by a \\w . 我想要添加一些文字填充来替换某些单词(总是\\w+ ),但前提是它没有被\\w包围。 (That does sound like a negative lookaround, but I hear JS doesn't do those!?) (这听起来像是一个负面的看法,但我听说JS不会做那些!?)

(Btw: the above "gimme" is the word literal I want to replace. If that wasn't obvious.) (顺便说一下:上面的“gimme”是我要替换的字面文字。如果那不明显。)

It has to be (?) a lookaround, because the \\W have to be zero-width, because the intention is a .replace(...) and I cannot replace/copy the surrounding characters. 它必须是(?)一个环视,因为\\W必须是零宽度,因为意图是.replace(...)而我无法替换/复制周围的字符。

So this won't work: 所以这不起作用:

text.replace(/(?=\W)(gimme)(?=\W)/gi, function(l, match, r) {
  return l + doMagic(match) + r;
})

The zero-width chars have to be ignored, so the function can return (and replace) only doMagic(match) . 零宽度字符必须被忽略,所以该函数可以返回(和替换) doMagic(match)

I have only very limited lookaround experience and non of it in JS. 我只有非常有限的外观经验,而不是JS。 Grazie. 感恩教堂。

PS. PS。 Or maybe I need a lookbehind and those aren't supported in JS..? 或者我可能需要一个lookbehind,那些在JS中不受支持..? I'm confused? 我糊涂了?

PS. PS。 A little bit of context: http://jsfiddle.net/rudiedirkx/kMs2N/show/ (ooh a link!) 一点上下文: http//jsfiddle.net/rudiedirkx/kMs2N/show/ (哦链接!)

  • you can use word boundary shortcut \\b to assert that it's the whole word that you are matching. 您可以使用单词边界快捷键\\b来断言它是您匹配的整个单词。

  • The easiest way to achieve what you want to do is probably to match: 实现您想要做的最简单的方法可能是匹配:

/(\\s+gimme)(?=\\W)/gi

and replace with [yourReplacement] - ie capture the whitespaces before 'gimme' and then include one in the replacement. 并用[yourReplacement]替换 - 即在'gimme'之前捕获空格,然后在替换中包含一个空格。

  • Another way to approach this would be capturing more characters before and after the gimme literal and then using the groups with backreference: 另一种方法是在gimme文字之前和之后捕获更多字符,然后使用带反向引用的组:

(\\W+?)gimme(\\W+?) - your match - note that this time the before and after characters are in the capturing groups 1 and 2 (\\W+?)gimme(\\W+?) - 您的匹配 - 请注意,此时前后字符位于捕获组1和2中

And you'd want to use \\1[yourReplacement]\\2 as replacement string - not sure how you use backreference in JS, but the idea is to tell the engine that with \\1 you mean whatever was matched by the first captuing parenthesis. 并且你想要使用\\1[yourReplacement]\\2作为替换字符串 - 不确定如何在JS中使用反向引用,但是想法是告诉引擎使用\\1表示第一个捕获括号匹配的内容。 In some languages these are accessed with $1 . 在某些语言中,这些是用$1访问的。

What you currently have will not work, for the following reason, (?=\\W) means "the next character is not a word character", and the next thing you try to match is a literal g , so you have a contradiction ("next character is a g , but isn't a letter"). 你现在拥有的东西是行不通的,因为以下原因, (?=\\W)意味着“下一个字符不是单词字符”,你想要匹配的下一个字是字面g ,所以你有一个矛盾( “下一个字符是g ,但不是字母”)。

You do in fact need a lookbehind, but they are not supported by JavaScript. 你确实需要一个lookbehind,但JavaScript不支持它们。

Check out this article on Mimicking Lookbehind in JavaScript for a possible approach. 查看有关JavaScript中Mimicking Lookbehind的文章,了解可能的方法。

Have you considered using a lexer/parser combo? 你考虑过使用词法分析器/解析器组合吗?

This one is javascript based, and comes with a spiffy demonstration . 这个是基于javascript的,并带有一个漂亮的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM