javascript - 在 Regex 问题 : TypeError: cannot read property "1" from null 中需要帮助

标签 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; 
}

我从来没有使用过 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/

相关文章:

java - 以编程方式将 Unicode 转换为转义的 Unicode

Javascript - 使用 map 重构(if 语句、意大利面条代码)

javascript - 如何计算一个变量并再次倒数javascript

javascript - Jquery .replace 使浏览器崩溃

mysql - 如何删除字符串的格式部分?

java - Java 是否有一种有效的方法在 StringBuilder 上执行多个正则表达式 ReplaceAll 操作?

javascript - JQ列表中的按钮

javascript - 如何获取 DIV 的值

具有 unicode 范围的 Python 正则表达式匹配不在范围内的字符

c++ - mbtowc 在 osx 上始终返回一个字节