1

So I found this question, which provides some great insight into the problem of smart punctuation from iOS, but I want to do the replacements wholesale, i.e. on a complete string, not as input is being entered by the user.

I looked around and it seems as though unicode characters are supported in ES6 through using \u<<code point>>. Does this work on matching with regex?

I've got this string:

"This is a message from iOS with it’s so-called “smart” punctuation.

I have been trying a test replacement with this:

x.replace(/\u8220/g, 'XXXX');

and I can verify that code point is in the string (via dev tools JS console):

> x.charCodeAt(47);
8220

But the output from my call to replace is the same string, unchanged. I've also tried wrapping the unicode part in [] but with no luck. Am I being dumb or is this just not supported in this manner?

Matt Lacey
  • 8,227
  • 35
  • 58

1 Answers1

2

8220 is the decimal value for the left double quote. To use \uXXXX you need to specify the hex value of the codepoint, in this case \u201C.

Here's an expression that would replace all of the “curly” quotes in a string with "straight" quotes:

s.replace(/[\u2018\u2019\u201C\u201D]/g, (c) => '\'\'""'.substr('\u2018\u2019\u201C\u201D'.indexOf(c), 1));
kshetline
  • 12,547
  • 4
  • 37
  • 73
  • Son of a.... I knew it'd be something stupid! I noticed other questions were using hex values and incorrectly assumed 8020 was hex. – Matt Lacey Mar 14 '18 at 03:49