axi*_*rem 1 javascript regex unicode
function numericEntityToChar(s) {
//s="登入"
console.log(s);
var chars = String.fromCharCode(s.match(/^&#(\d+);$/)[1]);
// throws uncaught exception TypeError: cannot read property "1" from null.
console.log(chars);
return chars;
}
Run Code Online (Sandbox Code Playgroud)
我从来没有和REGEX合作过,而且这个也没有作为第一个帮助.需要帮助.
您可以尝试这样设置:
function numericEntityToChar(s) {
var re = /&#(\d+);/g,
ret = "", match;
while (match = re.exec(s)) {
ret += String.fromCharCode(match[1]);
}
return ret;
}
var str = "登入";
console.log(numericEntityToChar(str));
Run Code Online (Sandbox Code Playgroud)
演示: http ://jsfiddle.net/XDGk9/
在^与$锚定不会让全球/多匹配和正则表达式不会与返回的实际数字.match().