[英]Javascript: exclude from regex in replace
In this regex: 在此正则表达式中:
let str = "hello 〔world〕,foo、bar。";
str.replace(/(〔(.*?)〕|(、)|(,)|(。))/gi,'<div>$1</div>');
How to exclude these two square brackets "〔" and "〕" from the result ? 如何从结果中排除这两个方括号“〔”和“〕”?
In order to get this result: 为了得到这个结果:
"hello <div>world</div><div>,</div>foo<div>、</div>bar<div>。</div>"
Instead of using a single group you can use two groups. 可以使用两个组来代替使用单个组。 and in callback based on group you can return the value accordingly
在基于组的回调中,您可以相应地返回值
let str = "hello 〔world〕,foo、bar。"; str = str.replace(/〔(.*?)〕|((…)|(。)|(,)|(、))/gi,(match,g1,g2)=>`<div>${g1 ? g1 : g2}</div>`); console.log(str)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.