美文网首页
二、字符串和正则表达式

二、字符串和正则表达式

作者: 入秋未凉的海 | 来源:发表于2017-09-14 23:52 被阅读0次
    更好的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;
        }
    }
    

    相关文章

      网友评论

          本文标题:二、字符串和正则表达式

          本文链接:https://www.haomeiwen.com/subject/amsjsxtx.html