简体   繁体   English

Javascript正则表达式:相同模式中匹配和不匹配的混合

[英]Javascript Regular Expression: Mix of matching and non-matching in the same pattern

I had been working with a few regular expressions and I ran into a problem. 我一直在使用一些正则表达式,但遇到了一个问题。

I need to run a replace on the following pattern: 我需要在以下模式下运行替换:

The string must have a \\E\\ but it must not be preceded by \\.br or a \\R 字符串必须带有\\ E \\,但不能以\\ .br或\\ R开头

Sample text: 示范文本:

Random\E\Text  - should match

\E\Text        - should match

\R\E\.br\      - should not match

\.br\E\R\      - should not match

Could anyone suggest a pattern that would be able to accomplish this? 任何人都可以提出一种可以完成此任务的模式吗?

I tried only for the \\.br case and while 我只在\\ .br情况下尝试过

/[^\b\\\.br\b]\E\/g

fails terribly with good reason, the following pattern seemed to not work either: 由于有充分的理由而严重失败,以下模式似乎也不起作用:

/(?!\\\.br)\E\/g

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks. 谢谢。

You could try (\\\\.br|\\\\R)\\\\E(\\\\.br|\\\\R) 您可以尝试(\\\\.br|\\\\R)\\\\E(\\\\.br|\\\\R)

Javascript doesn't support lookbehinds, but you should be able to negatively assert a match (if !match, string is valid). Javascript不支持回溯,但是您应该能够否定一个匹配项(如果!match,则字符串有效)。

Working example: http://regex101.com/r/hL3wN5 工作示例: http : //regex101.com/r/hL3wN5

var str = "Random\\E\\Text";
if (!(/(\\.br|\\R)\\E(\\.br|\\R)/g).test(str))
    //do stuff because it doesn't test true

Unfortunately, JavaScript does not support lookbehinds, only lookaheads. 不幸的是,JavaScript不支持先行查找,仅支持先行。 However, all is not lost! 但是,一切并没有丢失! You can a function for replacement: 您可以使用替换功能:

input.replace( /(...|..|.|$)\\E\\/, function( match, prefix ) {
    if( prefix==='\.br' || prefix.match( /\\R$/ ) )
        return match;  // return match unaffected
    else
        return prefix + '<\\E\\ replacement>';
});

I generally find that a lot of regex problems can be solved by this kind of approach: instead of doing everything in one giant regex, break it up into manageable chunks, and do multiple regexes. 我通常发现,通过这种方法可以解决很多正则表达式问题:与其在一个大型正则表达式中完成所有工作,不如将其分解为可管理的大块,然后执行多个正则表达式。

What's happening here is that the alternation operator is being used to look for the following patterns (in order): 这里发生的是交替运算符被用来寻找以下模式(按顺序):

...\\E\\        (catch potential instance of \.br\E\)
..\\E\\         (catch potential instance of \R\E\)
.\\E\\          (catch \E\ if it's at index 1 in the string)
$\\E\\          (catch \E\ if it's at index 0 in the string)

Once you've caught all of those possibilities, you can just examine what it was that was picked up before the \\E\\ . 一旦掌握了所有这些可能性,就可以检查\\E\\之前拾取的内容。 If it's \\.br or \\R , you just return the whole match (including the characters before). 如果它是\\.br\\R\\.br返回整个匹配项(包括前面的字符)。 If it's anything else, you can return the prefix followed by a replacement for \\E\\ . 否则,您可以返回前缀,后跟\\E\\

I hope this explanation makes sense! 我希望这种解释是有道理的!

Maybe this 也许这个

 # /^(?=.*\\E\\)(?!.*\\\.br\\E\\)(?!.*\\R\\E\\).+$/

 ^ 
 (?= .*  \\E\\ )
 (?! .*  \\\.br\\E\\ )
 (?! .*  \\R\\E\\ )
 .+                   // can be excluded if just a test
 $                    // can be excluded if just a test

Edit if that test matches, run the global replacement for /\\\\E\\\\/g its simple and fast. 编辑该测试是否匹配,以简单快捷的方式运行/\\\\E\\\\/g的全局替换。

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

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