简体   繁体   English

正则表达式替换括号中的文本

[英]Regex to replace text enclosed in brackets

The runtime is NodeJS, I'm trying to replace the text $Contact with a name.运行时是 NodeJS,我正在尝试用名称替换文本$Contact But I also need the ability to capture words around the term enclosed in curly brackets {} :但我还需要能够捕获包含在大括号{}中的术语周围的单词:

Where Name = John:
  My name is $Contact => My name is John
  Hello {I am $Contact} => Hello I am John

Where Name = null:
  My name is $Contact => My name is 
  Hello {I am $Contact} => Hello

The idea is to replace the $Contact qualifier with a string, and OPTIONALLY the text enclosed in brackets is only displayed if the string is not empty.这个想法是用字符串替换$Contact限定符,并且可选地,括号中的文本仅在字符串不为空时才显示。 The use of curly brackets is optional.大括号的使用是可选的。

// m = Message template (Hello {I am $Contact})
// qualifier = $Contact
// value = value to replace with

const replaceQualifier = (m, qualifier, value) =>
    m.replace(new RegExp('\\{?(.*?)\\' + qualifier + '(.*?)\\}?', 'g'), value ? `$1${value}$2` : '');

It seems to work for qualifiers without curly brackets but not with curly brackets.它似乎适用于没有大括号但不适用于大括号的限定符。

You need to use你需要使用

m.replace(new RegExp('(?:\\{([^{}]*))?\\' + qualifier + '(?:([^{}]*)})?', 'g'), 
   value ? `$1${value}$2` : '');

The regex will look like正则表达式看起来像

(?:\{([^{}]*))?\$Contact(?:([^{}]*)})?

See the regex demo .请参阅正则表达式演示 Details :详情

  • (?:\{([^{}]*))? - an optional occurrence of { and then (Group 1) any 0 or more chars other than { and } - 可选出现{ ,然后(第 1 组)除{}之外的任何 0 个或多个字符
  • \$Contact - $Contact \$Contact - $Contact
  • (?:([^{}]*)})? - an optional occurrence of (Group 2) any 0 or more chars other than { and } and then } . - 可选出现(第 2 组)除{}之外的任何 0 个或多个字符,然后是}

See the JS demo:参见 JS 演示:

 const replaceQualifier = (m, qualifier, value) => m.replace(new RegExp('(?:\\{([^{}]*))?\\' + qualifier + '(?:([^{}]*)})?', 'g'), value? `$1${value}$2`: ''); const qualifier = '$Contact'; let m = 'Hello {I am $Contact}'; let value = 'John'; console.log(replaceQualifier(m, qualifier, value)); value = ''; console.log(replaceQualifier(m, qualifier, value)); m = 'My name is $Contact'; value = 'John'; console.log(replaceQualifier(m, qualifier, value)); value = ''; console.log(replaceQualifier(m, qualifier, value));

Try this solution试试这个解决方案

 var str1 = "My name is $Contact" var str2 = "My name is {$Contact}" var str3 = "Hello {I am $Contact}" const replaceQualifier = (m, qualifier, value) => m.replace(new RegExp(`(?:\\{(.*?))?\\${qualifier}(?:(.*?)\\})?`), value? `$1${value}$2`: ''); console.log(replaceQualifier(str1,"$Contact","John")) console.log(replaceQualifier(str2,"$Contact","John")) console.log(replaceQualifier(str3,"$Contact","John")) console.log(replaceQualifier(str1,"$Contact")) console.log(replaceQualifier(str2,"$Contact")) console.log(replaceQualifier(str3,"$Contact"))

Output: Output:

My name is John
My name is John
Hello I am John
My name is 
My name is 
Hello 

The solution is very similar to yours, just the groups are used differently:该解决方案与您的解决方案非常相似,只是组的使用方式不同:

  • (?...) ... non-capturing ("plain") parentheses (?...) ... 非捕获(“普通”)括号
  • \\{(.*?) ... { followed by a captured group (.*?) \\{(.*?) ... {后跟捕获组(.*?)
  • (?:\\{(.*?))? ... present zero or more times ...出现零次或多次
  • similarly for the right part右边部分同样
EDIT (why the original regex did not work) 编辑(为什么原来的正则表达式不起作用)

while debugging replace , look first what is match ed, for debugging purposes without the g flag.在调试replace时,首先查看match的是什么,以便在没有g标志的情况下进行调试。 In this case, \{?(.*?) = optional { character, then (grouped) zero or more of any character and then the qualifier , ie the group is entire string before qualifier (because the { is optional )在这种情况下, \{?(.*?) =可选{字符,然后(分组)零个或多个任意字符,然后是qualifier ,即该组是qualifier之前的整个字符串(因为{可选的)

In the proposed solution, the entire block \{(.*?) is made optional, ie matching the {... part of the string if present, but does not match anything otherwise.在建议的解决方案中,整个块\{(.*?)是可选的,即匹配字符串的{...部分(如果存在),但不匹配其他任何内容。

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

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