ComixRu
ComixRu

Reputation: 73

Replace quotes by guillemets

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.

Upvotes: 2

Views: 296

Answers (1)

Mosh Feu
Mosh Feu

Reputation: 29277

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

Upvotes: 5

Related Questions