二、字符串和正则表达式

更好的Unicode支持
  • 一直基于16位字符编码(UTF-16)
  • 基本多文种平面(BMP)
let text = "𠮷"

console.log(text.length)//2
console.log(/^.$/.test(text))//false
console.log(text.charAt(0));//""不表示任何可打印的字符
console.log(text.charAt(1));//""
console.log(text.charCodeAt(0));//55362  每个166位编码单元对应的数值
console.log(text.charCodeAt(1));//57271

//非BMP字符 返回不同
let text = "𠮷a"
console.log(text.charCodeAt(0));//55362  
console.log(text.charCodeAt(1));//57271
console.log(text.charCodeAt(2));//97

console.log(text.codePointAt(0))//134071
console.log(text.codePointAt(1))//57271
console.log(text.codePointAt(2))//97

function is32Bit(c) {
    return c.codePointAt(0) > 0xFFFF;
}
console.log(is32Bit("𠮷")) //true
console.log(is32Bit("a")) //false

console.log(String.fromCodePoint(134071));//"𠮷"  fromCharCode

let normalized = values.map(function(){
    return text.normalize();
})

normalized.sort(function(first, second){
    if(first < second){
        return -1;
    } else if (first === second) {
        return 0;
    } else {
        return 1;
    }
})

//
values.sort(function(first, second){
    let firstNormalized = first.normalized();
    let secondNormalized = second.normalized();
    if(firstNormalized < secondNormalized) {
        return -1;
    } else if(firstNormalized === secondNormalized){
        return 0;
    } else {
        return 1
    }
})


//
values.sort(function(first, second){
    let firstNormalized = first.normalized("NFD");
    let secondNormalized = second.normalized("NFD");
    if(firstNormalized < secondNormalized) {
        return -1;
    } else if(firstNormalized === secondNormalized){
        return 0;
    } else {
        return 1
    }
})


//
let text = "𠮷"

console.log(text.length);//2
console.log(/^.$/.test(text));//false
console.log(/^.$/u.test(text));//true


//效率低

function codePointLength(text){
    let result = text.match(/[\s\S]/gu);
    return result?result.length:0;
}
console.log(codePointLength("abc"));//3
console.log(codePointLength("𠮷bc"));

//老式Javascript引擎中

function hasRegExp() {
    try {
        var pattern = new RegExp(".","u");
        return true;
    } catch(ex) {
        return false;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容