Replace quotes by guillemets

Viewed 107

How can i replace quotes ("") in text with guillemets (« and »)? I tried the usual method (replace with regexp) for replacing, but it turned out badly - I do not know how to determine if an opening quote or a closing quote.

1 Answers

You can do replace with a regex - /"(.*?)"/g to replace the whole "word".

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

Or

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

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

Related