简体   繁体   English

Javascript正则表达式负提前匹配

[英]Javascript Regex Negative Lookahead match

I suck at regex. 我在正则表达式上很烂。 I have the following expression: 我有以下表达:

(?!.*\\")(Level)(\\s)([0-9])(?!.*\\")

I want to use this expression to replace all Level 2 to "Level 2" with queries. 我想使用此表达式用查询将所有2级替换为“ 2级”。 The problem is that this regex catches "Level 2 (without closing " ). 问题是该正则表达式捕获"Level 2 (不关闭" )。 I want to catch Level 2 only if it has to quotes before AND after the words. 我只想在Level 2必须在单词之前和之后加引号的情况下才能捕获该Level 2

What is the best approach to do that? 最好的方法是什么?

If you just want to capture every occurrence of Level\\s+\\d+ and replace it with "Level\\s+\\d+" provided it is not enclosed by doublequotes from both sides, you may use try using this regex, 如果您只想捕获每一次出现的Level\\s+\\d+并将其替换为"Level\\s+\\d+"前提是它没有被双引号引起来,则可以尝试使用此正则表达式,

([^"]|^)(Level\s+\d+)([^"]|$)

and replace it with, 并替换为

\1"\2"\3

Check here for a demo 在这里查看演示

This will not choose a text for replacement of type Label 2" or "Label 2 or "Label 2" and only replace when Label 2 when it is not surrounded by double quote from either side. 这将不会选择替换“ Label 2""Label 2"Label 2"类型的文本,仅当Label 2两侧没有双引号包围时才进行替换。

You may use 您可以使用

 var s = '"Level 3 protected": Level 4 here and "another Level 5"'; s = s.replace(/(^|[^"])(Level\\s+\\d+)(?!["\\d])/g, '$1"$2"'); console.log(s); 

Details 细节

  • (^|[^"]) - Group 1 ( $1 ): start of string or any char but a double quote (^|[^"]) -组1( $1 ):字符串的开头或除双引号外的任何字符
  • (Level\\s+\\d+) - Group 2 ( $2 ): Level , 1+ whitespaces, 1+ digits (Level\\s+\\d+) -组2( $2 ): Level ,1+个空格,1+个数字
  • (?!["\\d]) - no digit or double quote to the left of the current location is allowed. (?!["\\d]) -当前位置的左侧不允许使用数字或双引号。

See the regex demo . 参见regex演示

Not sure if you want to replace "Level 2 and Level 2" or not, so I offer solutions for both scenarios: 不知道是否要替换"Level 2"Level 2 Level 2" ,因此我提供了两种方案的解决方案:

1) If you want to replace only those with not even a single " : 1) 如果您只想替换甚至没有一个"

(?<!")(Level\\s\\d+)(?!")

see https://regex101.com/r/Nc7b5h/1 参见https://regex101.com/r/Nc7b5h/1

This uses a negative look-behind and a negative look-ahead 这使用负向后看和负向前看


2) However, if you also want to replace those with only one " , try this: 2) 但是,如果您还想仅用一个 "替换那些" ,请尝试以下操作:

(?!"Level\\s\\d+")(?<!")"?Level\\s\\d+"?

https://regex101.com/r/qL5Li5/1/ https://regex101.com/r/qL5Li5/1/

Set your match group based on whether you want to replace the single " or not: 根据您是否要替换单个"来设置匹配组:

var s='This "Level 2" is "Level 2 a Level 2" test Level 2 :)'
​
s.replace(/(?!"Level\s\d+")(?<!")"?(Level\s\d+)"?/g, '"$1"');

//> 'This "Level 2" is "Level 2" a "Level 2" test "Level 2" :)'

or 要么

var s='This "Level 2" is "Level 2 a Level 2" test Level 2 :)'

s.replace(/(?!"Level\s\d+")(?<!")("?Level\s\d+"?)/g, '"$1"');

//> 'This "Level 2" is ""Level 2" a "Level 2"" test "Level 2" :)'

The trick here was to first perform a negative look-ahead for the forbidden case of a match with two " , before doing anything else. 这里的窍门是,在执行其他任何操作之前,首先对两个 "匹配的禁止情况进行否定的超前查找。

General Notes 一般注意事项

If you don't want to use a look-behind expression ( (?<!") ) as older browsers (pre-ES2018) won't support that, just use (^|[^"]) instead, as other suggested. 如果您不想使用后向表达式( (?<!")因为较旧的浏览器(ES2018之前的版本)将不支持该表达式,则可以使用(^|[^"])代替,如其他建议。 Just don't forget to restore its contents in the replacement :) 只是不要忘记在替换中恢复其内容:)

eg s.replace(/(?!"Level\\s\\d+")(^|[^"])"?(Level\\s\\d+)"?/g, '$1"$2"'); 例如s.replace(/(?!"Level\\s\\d+")(^|[^"])"?(Level\\s\\d+)"?/g, '$1"$2"');

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

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