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;
}
我从来没有使用过 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));
演示: http://jsfiddle.net/XDGk9/
^
和 $
anchor 不允许全局/多重匹配,并且正则表达式不会使用 .match()< 返回实际数字
.
关于javascript - 在 Regex 问题 : TypeError: cannot read property "1" from null 中需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16592655/