What is a surrogate pair?

前端 未结 3 629
猫巷女王i
猫巷女王i 2021-01-03 03:49

I came across this code in a javascript open source project.

validator.isLength = function (str, min, max) 
    // match surrogate pairs in string or declare         


        
3条回答
  •  鱼传尺愫
    2021-01-03 03:53

    1. Yes. Your understanding is correct. The function returns the length of the string in Unicode Code Points.

    2. JavaScript is using UTF-16 to encode its strings. This means two bytes (16-bit) are used to represent one Unicode Code Point.

      Now there are characters (like the Emojis) in Unicode that have a that high code point so that they cannot be stored in 2 bytes (16bit) so they need to get encoded into two UTF-16 characters (4 bytes). These are called surrogate pairs.

    Try this

    var len = "

提交回复
热议问题