简体   繁体   English

正则表达式替换字符串中不在引号中的单词

[英]Regex replace word in string that are not in quotes

I'm looking to do replacements in unknown third-party inputs in strings that sometimes have quotes among them.我正在寻找在有时带有引号的字符串中替换未知的第三方输入。

I want to replace a wholeword whereever it occurs unless it's in double or single-quotes, and unless the quote is escaped.我想替换出现的整个单词,除非它在双引号或单引号中,并且除非引号被转义。

Example: Replacing FOO by BAR示例:用 BAR 替换 FOO

Input:输入:

FOO "FOO" 'FOO' "    1 + FOO + 2 " ABCFOOXYZ "  str1\"FOO\"str3'FOO'\'\'" '  str1\'FOOstr3"FOO"\"\"' \"FOO\"

Expected output:预计 output:

BAR "FOO" 'FOO' "    1 + FOO + 2 " ABCFOOXYZ "  str1\"FOO\"str3'FOO'\'\'" '  str1\'FOOstr3"FOO"\"\"' \"BAR\"

More tests:更多测试:

name: FOO
favoriteQuote: "I am my own FOO."
children: 'FOO\'s children'
cars: ownersList[FOO]
statement = FOO + " is the owner of the house."

Expected output:预计 output:

name: BAR
favoriteQuote: "I am my own FOO."
children: 'FOO\'s children'
cars: ownersList[BAR]
statement = BAR + " is the owner of the house."

I saw this question: Match and replace a word not in quotes (string contains escaped quotes) which I thought was similar and could be a good starting point the accepted answer does not work at all:我看到了这个问题: 匹配并替换一个不在引号中的单词(字符串包含转义引号) ,我认为这很相似并且可能是一个很好的起点接受的答案根本不起作用:

https://regex101.com/r/Lfan64/5 https://regex101.com/r/Lfan64/5

If anyone could help me get the expected result from my regex that would be great, thanks.如果有人可以帮助我从我的正则表达式中获得预期的结果,那就太好了,谢谢。

If I am understanding your requirements correctly, you may try this regex for your cases:如果我正确理解您的要求,您可以为您的案例尝试这个正则表达式:

((['"])(?:\\.|(?!\2).)*(?<!\\)\2)|\bFOO\b/g

Updated RegEx Demo更新了 RegEx 演示

This regex uses alternation to match and discard what we need to keep on LHS of |这个正则表达式使用交替来匹配和丢弃我们需要保留在|的 LHS 上的内容。 whereas on RHS we match whatever we want to replace in the result.而在 RHS 上,我们匹配我们想要在结果中替换的任何内容。

Code:代码:

 const str = String.raw`name: FOO favoriteQuote: "I am my own FOO." children: 'FOO\'s children' cars: ownersList[FOO] statement = FOO + " is the owner of the house." FOO "FOO" 'FOO' " 1 + FOO + 2 " ABCFOOXYZ " str1\"FOO\"str3'FOO'\'\'" ' str1\'FOOstr3"FOO"\"\"' \"FOO\"`; var repl = str.replace(/((['"])(?:\\.|(?.\2)?)*(,<,\\)\2)|\bFOO\b/g; (_. g) => g || "BAR"); console.log(repl);

RegEx Details:正则表达式详细信息:

  • ( : Start capture group #1 ( : 开始捕获组#1
    • (['"]) : Match ' or " in capture group #2 (['"]) :匹配捕获组 #2 中'"
    • (?:\\.|(?.\2).)* : Match an escaped character or any character except the quote we matched in capture group #2 (?:\\.|(?.\2).)* :匹配转义字符或除我们在捕获组 #2 中匹配的引号之外的任何字符
    • (?<!\\)\2 : Match whatever quote we matched in capture group #2 as long as it is not preceded by a \ (?<!\\)\2 :匹配我们在捕获组 #2 中匹配的任何引号,只要它前面没有\
  • ) : End capture group #1 ) : 结束捕获组#1
  • | : OR : 或者
  • \bFOO\b : Match complete word FOO \bFOO\b : 匹配完整的单词FOO

You can use您可以使用

 const text = String.raw`name: FOO favoriteQuote: "I am my own FOO." children: 'FOO\'s children' cars: ownersList[FOO] statement = FOO + " is the owner of the house." FOO "FOO" 'FOO' " 1 + FOO + 2 " ABCFOOXYZ " str1\"FOO\"str3'FOO'\'\'" ' str1\'FOOstr3"FOO"\"\"' \"FOO\"` console.log( text.replace( /((?:[^\\]|^)(?:\\{2})*(?:"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*'))|FOO/g, (match, group) => group || "BAR" ))

Details :详情

  • ((?:[^\\]|^)(?:\\{2})*(?:"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*')) - Group 1: ((?:[^\\]|^)(?:\\{2})*(?:"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*')) - 第 1 组:
    • (?:[^\\]|^) - a char other than \ or start of string (?:[^\\]|^) - \以外的字符或字符串开头
    • (?:\\{2})* - zero or more sequences of double backslash (?:\\{2})* - 零个或多个双反斜杠序列
    • (?:"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*') - either of a double or single quoted string literal pattern with escape sequence support (?:"[^"\\]*(?:\\[^][^"\\]*)*"|'[^'\\]*(?:\\[^][^'\\]*)*') - 支持转义序列的双引号或单引号字符串文字模式
  • | - or - 或者
  • FOO - a FOO string in any other context. FOO - 任何其他上下文中的FOO字符串。

The (match, group) => group || "BAR" (match, group) => group || "BAR" (match, group) => group || "BAR" replacement means that if Group 1 matches, the replacement is Group 1 value, else, the replacement is BAR . (match, group) => group || "BAR"替换意味着如果第 1 组匹配,则替换为第 1 组值,否则,替换为BAR

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

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