0
const x = "Amanda’s normally placid temperament ��新道 reasserted itself."

const y = `x.replace(/[\u{0080}-\u{FFFF}]/gu, '');` 

console.log(y); // Amandas normally placid temperament  reasserted itself.

This removes the non-ASCII characters(desired), along with the apostrophe too (not desired).

How can I modify the regex such that it does not remove the following:

U+0022 - Apostrophe
U+2019 - Right single quotation mark
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Kaya Toast
  • 5,267
  • 8
  • 35
  • 59
  • See [this answer](https://stackoverflow.com/a/17328119/3832970) in the linked thread, under *All other flavors* – Wiktor Stribiżew Nov 25 '19 at 08:02
  • x.replace(/(?!')[\u{0080}-\u{FFFF} ']/gu, '') ... This replaces all the spaces too (undesired) – Kaya Toast Nov 25 '19 at 08:12
  • Yes, because you added a space to the character class. If you do not need that behavior, remove it – Wiktor Stribiżew Nov 25 '19 at 08:13
  • x.replace(/(?!')[\u{0080}-\u{FFFF}]/gu, '') ... doesn't do much. Doesn't remove the apostrophe – Kaya Toast Nov 25 '19 at 08:15
  • Look, you have `[\u{0080}-\u{FFFF}]`. To add the exceptions you have, just add a lookahead, `(?![\u{2019}])[\u{0080}-\u{FFFF}]` (and you may add more chars to the character class in the lookahead). The `U+0022 - Apostrophe` does not have to be added, as it is out of the range. If it does not do what you need, your question should be modified to clarify what you are doing and need to achieve – Wiktor Stribiżew Nov 25 '19 at 08:20
  • Got it. Thanks. It is best to delete this question then, right ? To avoid confusion ? – Kaya Toast Nov 25 '19 at 08:21
  • You do not have to. – Wiktor Stribiżew Nov 25 '19 at 08:23

0 Answers0