简体   繁体   English

获取捕获组之间的文本

[英]Get text between capture groups

Suppose I am given a regex like the following: 假设我得到如下正则表达式:

(\/*).*?(\*/)

Using this regex, I need to transform some text from: 使用此正则表达式,我需要将一些文本转换为:

/* This is a comment */

To: 至:

([/*] This is a comment [*/])

Both the regex and the transformation rules are given to me; 正则表达式和转换规则都给了我。 I can't ask for a different regex format. 我不能要求其他正则表达式格式。

I could do this easily if I could reliably split text into a sequence of capture groups and non-captured text. 如果可以将文本可靠地分为一系列捕获组和未捕获的文本,则可以轻松完成此操作。 However, this doesn't seem to be possible in general using javascript regexes, because exec does not save information about the indexes of individual matches. 但是,使用javascript正则表达式通常似乎不太可能,因为exec不会保存有关单个匹配项的索引的信息。 Is there a solution? 有解决方案吗?

Use a regexp to transform your regexp by adding additional capture groups: 使用正则表达式通过添加其他捕获组来转换正则表达式:

 function addCapture(reg) { return new RegExp(reg.source.replace(/\\(.*?\\)|[^(]*/g, match => match[0] === '(' ? match : `(${match})`), reg.flags); } const regexp = /(\\/\\*).*?(\\*\\/)/; const input = "/* This is a comment */"; console.log(input.replace(addCapture(regexp), '[$1]$2[$3]')); 

Eg using String.prototype.replace() , you can refer the capturing groups by their indices: 例如,使用String.prototype.replace() ,可以通过捕获组的索引来引用捕获组:

 var input = '/* This is a comment */' var output = input.replace(/(\\/\\*)(.*)(\\*\\/)/, '([$1]$2[$3])') console.log(output); // "([/*] This is a comment [*/])" 

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

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