Skip to content

Instantly share code, notes, and snippets.

@MJ111
Created August 1, 2017 03:55
Show Gist options
  • Select an option

  • Save MJ111/47caea5f4bc2baf6e4213041c2d48e20 to your computer and use it in GitHub Desktop.

Select an option

Save MJ111/47caea5f4bc2baf6e4213041c2d48e20 to your computer and use it in GitHub Desktop.
[Remove divided surrogate pair unicode] #JavaScript #Unicode
/*
* This would be necessary If you have limited length textarea and sliced the input string.
* because individual surrogate codes are useless and raise error in Python and broken character is ugly
* (https://unicode-table.com/en/blocks/high-surrogates/)
*/
function SliceAndCleanInputStr (inputStr) { // inputStr = 'aaaaaa😄'
var sliced = inputStrstr.slice(0, 7) // "aaaaaa�"
return sliced.replace(/[\ud800-\udbff]/g, '') // "aaaaaa"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment