javascriptunicodeutf-16surrogate-pairs

Replace all unmatched surrogate pairs with replacement character in JavaScript string


I have a JavaScript string that I'm writing to a file. I need to replace any unmatched surrogate pairs with the replacement character. Is there some regex character class that only matches unpaired surrogates or do I have to do some additional processing?


Solution

  • String.prototype.toWellFormed() replaces any lone surrogates with the Unicode replacement character U+FFFD .