本文介绍了使用JavaScript正则表达式将数字HTML实体替换为其实际字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用JavaScript&正则表达式用他们的实际Unicode字符替换数字HTML实体,例如
I'm trying to use JavaScript & regex to replace numerical HTML entities with their actual Unicode characters, e.g.
foo's bar
→
foo's bar
这是我到目前为止所得到的:
This is what I got so far:
"foo's bar".replace(/&#([^\s]*);/g, "$1"); // "foo39s bar"
剩下要做的就是用<$ c $替换数字c> String.fromCharCode($ 1),但我似乎无法让它工作。我怎么能这样做?
All that's left to do is to replace the number with String.fromCharCode($1)
, but I can't seem to get it to work. How can I do this?
推荐答案
"foo's bar".replace(/&#(\d+);/g, function(match, match2) {return String.fromCharCode(+match2);})
这篇关于使用JavaScript正则表达式将数字HTML实体替换为其实际字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!