用guillemets替换引号

如何""用 guillemets ( «and »)替换文本中的引号 ( ) ?我尝试了通常的替换方法(用正则表达式替换),但结果很糟糕 - 我不知道如何确定是开头引用还是结尾引用。

回答

您可以replace使用正则表达式 -/"(.*?)"/g替换整个“单词”。

console.log(
  '"word1" word2 "word3"'.replace(/"(.*?)"/g, '«$1»'))

或者

console.log(
  '"word1" word2 "word3"'.replace(/"(.*?)"/g, (a, b) => `«${b}»`));

https://regex101.com/r/9s68XL/1


以上是用guillemets替换引号的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>