我试图在下面的字符串中替换笑脸
function decoder(text, done) {
async.parallel([
function (cback) {
// Thanks to @CMS
var emoticons = window.symbols,
patterns = [],
metachars = /[[\]{}()*+?.\\|^$\-,&#\s]/g;
// build a regex pattern for each defined property
for (var i in emoticons) {
if (emoticons.hasOwnProperty(i)) { // escape metacharacters
patterns.push('(' + i.replace(metachars, "\\$&") + ')');
}
}
// build the regular expression and replace
text = text.replace(new RegExp(patterns.join('|'), 'g'), function (match) {
return typeof emoticons[match] != 'undefined' ?
"<img src='/" + emoticons[key] + "' />" : match;
});
cback(null, true);
},
function (cback) {
var ref = window.smileys,
keys = Object.keys(ref);
async.each(keys, function (key, cb) {
text = text.replace(new RegExp('[' + key + ']', 'gi'), "<img src='/" + ref[key] + "' />");
cb(null);
}, function () {
cback(null, true);
});
}], function (err, res) {
done(text);
});
}但这会让页面崩溃。是否有任何特定的代码阻塞了这个问题,而调试时我已经观察到第二个函数正在阻塞它,但不明白为什么。微笑的总数是300。笑脸采用[Dancing]、[Kidding]、:)等格式。
发布于 2014-04-12 08:37:56
它没有使用下面技巧中使用的正则表达式,而是非常有效,这要感谢@James McLaughlin。
text = text.split(smileySymbol).join(imgHTML);https://stackoverflow.com/questions/23027740
复制相似问题