UTF-16 代码点
codePointAt() 方法
ES6为全面支持 UTF-16而新增的方法之一。
判断字符码元的栗子:
ES5:
var 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
console.log(text.charCodeAt(1)); // 57271
ES6:
var 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
此方法可以在给定字符串中按位置提取 Unicode 代码点。接受的是码元位置而非字符位置,并返回一个整数值.
codePointAt() 来提取字符串内中某个字符的代码点
String.fromCodePoint()
String.fromCodePoint() 用给定的代码点来产生包含单个字符的字符串
console.log(String.fromCodePoint(134071)); // ""
normalize()
记住,当比较字符串时,它们必须被标准化 为同一种形式
var normalized = values.map(function(text) {
return text.normalize();
});
normalized.sort(function(first, second) {
if (first < second) {
return -1;
} else if (first === second) {
return 0;
} else {
return 1;
}
});
或者 在比较过程中调用 normalize() 来对原始数组进行排序:
values.sort(function(first, second) {
var firstNormalized = first.normalize(),
secondNormalized = second.normalize();
if (firstNormalized < secondNormalized) {
return -1;
} else if (firstNormalized === secondNormalized) {
return 0;
} else {
return 1;
}
});
Tips:
如果你之前从未担心过Unicode 标准化方面的问题,那么可能暂时还不太会用到这个方法。 然而若你曾经开发过国际化的应用,你就一定会发现 normalize() 方法非常有用。
正则表达式 u 标志
var text = "";
console.log(text.length); // 2
console.log(/^.$/.test(text)); // false
console.log(/^.$/u.test(text)); // true
计算代码点数量
function codePointLength(text) {
var result = text.match(/[\s\S]/gu);
return result ? result.length : 0;
}
console.log(codePointLength("abc")); // 3 console.log(codePointLength("bc" )); // 3
此例调用了 match() 方法来检查 text 中的空白字符与非空白字符(使用 [\s\S] 以确保 该模式能匹配换行符),所用的正则表达式启用了全局与 Unicode 特性。在匹配至少成功一 次的情况下, result 变量会是包含匹配结果的数组,因此该数组的长度就是字符串中代码 点的数量。在 Unicode 中,字符串 "abc" 与 "bc" 同样包含三个字符,所以数组长度为 3。
虽然这种方法可用,但它并不快,尤其在操作长字符串时。你也可以使用字符串的迭代 器
一般来说,只要有可能就应尽量减少对代码点数量 的计算。
u 标志是一项语法变更,在不兼容 ES6 的 JS 引擎中试图使用它就会抛出语法错误。
使用一个函数来判断是否支持 u 标志:
function hasRegExpU() {
try {
var pattern = new RegExp(".", "u");
return true;
} catch (ex) {
return false;
}
}
识别子字符串的方法
识别字符串是否存在于其它字符串中:
indexOf() 方法,在给定文本存在于字符串中的任意位置时会返回第一次出现该文本的位置索引 ,否则返回-1 ;
includes() 方法,在给定文本存在于字符串中的任意位置时会返回 true ,否则返回
false ;
startsWith() 方法,在给定文本出现在字符串起始处时返回 true ,否则返回 false ;
endsWith() 方法,在给定文本出现在字符串结尾处时返回 true ,否则返回 false 。
var msg = "Hello world!";
console.log(msg.startsWith("Hello");
console.log(msg.endsWith("!"));
console.log(msg.includes("o"));
console.log(msg.startsWith("o", 4));console.log(msg.endsWith("o", 8));console.log(msg.includes("o", 8));
前三次调用,仅有一个参数,会搜索整个字符串;
后三次调用,有两个参数,从索引为第二个参数的位置开始搜索。
以上仅判断是否存在,返回布尔值;
若需要找到具体的位置需要使用indexOf() lastIndexOf().
repeat() 方法
它接受一个参数作为字符串的重复次数,返回 一个将初始字符串重复指定次数的新字符串。
console.log("x".repeat(3)); // "xxx"
console.log("hello".repeat(2)); // "hellohello"
console.log("abc".repeat(4)); // "abcabcabcabc"
方法比相同目的的其余方法更加方便,在操纵文本时特别有用,尤其是在需要产生缩进的 代码格式化工具中:
// indent 使用了一定数量的空格
var indent = " ".repeat(4),indentLevel = 0;
// 每当你增加缩进
var newIndent = indent.repeat(++indentLevel);
正则表达式的其他改动
y 标志影 响正则表达式搜索时的粘连( sticky )属性,它表示从正则表达式的 lastIndex 属性值的 位置开始检索字符串中的匹配字符。如果在该位置没有匹配成功,那么正则表达式将停止检 索。
var text = "hello1 hello2 hello3",
pattern = /hello\d\s?/,
result = pattern.exec(text),
globalPattern = /hello\d\s?/g,
globalResult = globalPattern.exec(text),
stickyPattern = /hello\d\s?/y,
stickyResult = stickyPattern.exec(text);
console.log(result[0]); // "hello1 "console.log(globalResult[0]); // "hello1 "console.log(stickyResult[0]); // "hello1 "
pattern.lastIndex = 1;
globalPattern.lastIndex = 1;
stickyPattern.lastIndex = 1;
result = pattern.exec(text);
globalResult = globalPattern.exec(text);
stickyResult = stickyPattern.exec(text);
console.log(result[0]); // "hello1
"console.log(globalResult[0]); // "hello2 "
console.log(stickyResult[0]); // Error! stickyResult is null
复制正则表达式
var re1 = /ab/i,
// ES5 中会抛出错误, ES6 中可用
re2 = new RegExp(re1, "g");
console.log(re1.toString()); // "/ab/i"
console.log(re2.toString()); // "/ab/g"
console.log(re1.test("ab")); // trueconsole.log(re2.test("ab")); // true
// re1 带有忽略大小写的 i 标志
// re2 则只带有全局的 g 标志
网友评论