我怎样才能捕获所有组并将它们替换为组的重新格式化版本?
How can I capture all groups and replace them with the reformatted version of the group?
文本输入:
some "single , quote" , text ""{one,2two space,three-dash,four}"" "{some,text}" ""{alpha,bravo space - dash,charlie}"" some text
文本输出:
some "single , quote" , text ""{one","2two space","three-dash","four}"" "{some,text}" ""{alpha","bravo space - dash","charlie}"" some text
我有下面的javascript解决方案,但我想知道是否有更好的解决方案?
const str = "some \"single , quote\" , text \"\"\{one,2two space,three-dash,four\}\"\" \"\{some,text\}\" \"\"\{alpha,bravo space - dash,charlie\}\"\" some text"
let res = str;
const matches = res.match(/""{([^"])*}""/g);
matches?.forEach( match => {
res = res.replace(match, match.replace(/,/g,'","'));
});
console.log(str); // TEXT IN
console.log(res); // TEXT OUT
你可以给一个函数作为 replace()
方法的替换参数,这样你就不需要循环了。
此外,您的正则表达式可以简化。您不需要捕获组,只需将 *
量词放在 [^"]
之后
const str = 'some, text ""{one,2two space,three-dash,four}"" some, text ""{alpha,bravo space - dash,charlie}"" some text';
res = str.replace(/""{[^"]*}""/g, match => match.replace(/,/g, '","'))
console.log(res);
您可以使用以下正则表达式来匹配要替换为(三字符字符串)'","'
.
的逗号
/(?<=""{[^{}\n]*),(?=[^{}\n]*}"")/
这是可能的,因为 Javascript 支持 可变长度 回顾。
对于字符串
some, text ""{one,2two space,three-dash,four}"" some, text ""{alpha,bravo space - dash,charlie}"" some text';
字符串中的匹配和替换结果
some, text ""{one","2two space","three-dash","four}"" some, text ""{alpha","bravo space - dash","charlie}"" some text';
Javascript 的正则表达式引擎执行以下操作。
(?<= : begin a positive lookbehind
""{ : match '""{'
[^{}\n]* : match 0+ chars other than those shown in the char class
) : end positive lookbehind
, : match ','
(?= : begin positive lookahead
[^{}\n]* : match 0+ chars other than those shown in the char class
}"" : match '}""'
) : end positive lookahead
文本输入:
some "single , quote" , text ""{one,2two space,three-dash,four}"" "{some,text}" ""{alpha,bravo space - dash,charlie}"" some text
文本输出:
some "single , quote" , text ""{one","2two space","three-dash","four}"" "{some,text}" ""{alpha","bravo space - dash","charlie}"" some text
我有下面的javascript解决方案,但我想知道是否有更好的解决方案?
const str = "some \"single , quote\" , text \"\"\{one,2two space,three-dash,four\}\"\" \"\{some,text\}\" \"\"\{alpha,bravo space - dash,charlie\}\"\" some text"
let res = str;
const matches = res.match(/""{([^"])*}""/g);
matches?.forEach( match => {
res = res.replace(match, match.replace(/,/g,'","'));
});
console.log(str); // TEXT IN
console.log(res); // TEXT OUT
你可以给一个函数作为 replace()
方法的替换参数,这样你就不需要循环了。
此外,您的正则表达式可以简化。您不需要捕获组,只需将 *
量词放在 [^"]
const str = 'some, text ""{one,2two space,three-dash,four}"" some, text ""{alpha,bravo space - dash,charlie}"" some text';
res = str.replace(/""{[^"]*}""/g, match => match.replace(/,/g, '","'))
console.log(res);
您可以使用以下正则表达式来匹配要替换为(三字符字符串)'","'
.
/(?<=""{[^{}\n]*),(?=[^{}\n]*}"")/
这是可能的,因为 Javascript 支持 可变长度 回顾。
对于字符串
some, text ""{one,2two space,three-dash,four}"" some, text ""{alpha,bravo space - dash,charlie}"" some text';
字符串中的匹配和替换结果
some, text ""{one","2two space","three-dash","four}"" some, text ""{alpha","bravo space - dash","charlie}"" some text';
Javascript 的正则表达式引擎执行以下操作。
(?<= : begin a positive lookbehind
""{ : match '""{'
[^{}\n]* : match 0+ chars other than those shown in the char class
) : end positive lookbehind
, : match ','
(?= : begin positive lookahead
[^{}\n]* : match 0+ chars other than those shown in the char class
}"" : match '}""'
) : end positive lookahead