Skip to content

Instantly share code, notes, and snippets.

@asbubam
Last active February 15, 2016 04:48
Show Gist options
  • Save asbubam/6e2e9b267c806183b0c6 to your computer and use it in GitHub Desktop.
Save asbubam/6e2e9b267c806183b0c6 to your computer and use it in GitHub Desktop.
JS에서 surrogate pair 를 포함한 문자열 자르기
> var str = '안녕하세요\ud83d\ude03하이';
undefined
> str
'안녕하세요😃하이'
> str.length
9
> encodeURI(str.slice(0,6))
URIError: URI malformed
at encodeURI (native)
at repl:1:15
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
> var decodedArr = punycode.ucs2.decode(str);
undefined
> var slicedStr = punycode.ucs2.encode(decodedArr.slice(0,6));
undefined
> slicedStr
'안녕하세요😃'
> encodeURI(slicedStr);
'%EC%95%88%EB%85%95%ED%95%98%EC%84%B8%EC%9A%94%F0%9F%98%83'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment