Javascript 使replace函数不区分大小写
我有一个功能,用图像表情代替文字笑脸等 我如何使它不区分大小写? 我曾尝试在替换程序中使用“gi”和“ig”,但似乎没有什么不同Javascript 使replace函数不区分大小写,javascript,jquery,replace,case-insensitive,Javascript,Jquery,Replace,Case Insensitive,我有一个功能,用图像表情代替文字笑脸等 我如何使它不区分大小写? 我曾尝试在替换程序中使用“gi”和“ig”,但似乎没有什么不同 var emots = { ':)' : 'smile', ':-)' : 'smile', ';)' : 'wink', ';-)' : 'wink', ':(' : 'downer', ':-(' : 'downer', ':D' : 'happy', ':-D' : 'happy', '(s
var emots = {
':)' : 'smile',
':-)' : 'smile',
';)' : 'wink',
';-)' : 'wink',
':(' : 'downer',
':-(' : 'downer',
':D' : 'happy',
':-D' : 'happy',
'(smoke)' : 'smoke',
'(y)' : 'thumbsup',
'(b)' : 'beer',
'(c)' : 'coffee',
'(cool)' : 'cool',
'(hehe)' : 'tooth',
'(haha)' : 'tooth',
'(lol)' : 'tooth',
'(pacman)' : 'pacman',
'(hmm)' : 'sarcastic',
'(woot)' : 'woot',
'(pirate)' : 'pirate',
'(wtf)' : 'wtf'
};
function smile(str){
var out = str;
for(k in emots){
out = out.replace(k,'<img src="/emoticons/'+emots[k]+'.gif" title="'+k+'" />','g');
}
return out;
};
var-emots={
“:)”:“微笑”,
“:-)”:“微笑”,
“;)”:“眨眼”,
“;-)”:“眨眼”,
':(':'downer',
':-(':'downer',
“:D”:“快乐”,
“:-D”:“快乐”,
"(烟雾)""烟雾",,
"(y)""拇指",,
"(二)""啤酒",,
"(三)""咖啡",,
"(酷)""酷",,
"(呵呵)"牙",,
"(哈哈)"牙",,
"(哈哈)"牙",,
"(吃豆人)""吃豆人",,
"(嗯)"讽刺",,
"(呜)""呜",,
"(海盗)""海盗",,
"(世贸论坛)":"世贸论坛"
};
功能微笑(str){
var out=str;
for(emots中的k){
out=out.替换(k,,'g');
}
返回;
};
更改:
out = out.replace(k,'<img src="/emoticons/'+emots[k]+'.gif" title="'+k+'" />','g');
out=out.replace(k,,'g');
致:
out=out.replace(新的RegExp(k.replace(/[-\[\]\/\{\})(\)\*\+\?\\\\\\\\^\$\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
这个答案中的正则表达式转义函数比表面上看起来要复杂一些。下面是一个完整的解决方案。为了简单和高效,它只对目标字符串使用一个正则表达式搜索 请注意,因为它不区分大小写(例如,
(hehe)
和(hehe)
被视为相同),:-d
也被视为与:-d
相同
var emots = {
':)' : 'smile',
':-)' : 'smile'
// Add the rest of your emoticons...
};
// Build the regex that will be used for searches
var emotSearch = [];
for (var p in emots) {
if (emots.hasOwnProperty(p)) {
emotSearch.push(p.replace(/[-[{()*+?.\\^$|]/g, '\\$&'));
if (p !== p.toLowerCase()) {
emots[p.toLowerCase()] = emots[p];
}
}
}
emotSearch = RegExp(emotSearch.join('|'), 'gi');
function smile(str) {
return str.replace(emotSearch, function ($0) {
var emot = $0.toLowerCase();
if (emot in emots) {
return '<img src="/emoticons/' + emots[emot] + '.gif" title="' + $0 + '" />';
}
return $0;
});
}
var-emots={
“:)”:“微笑”,
“:-)”:“微笑”
//添加其余的表情符号。。。
};
//生成将用于搜索的正则表达式
var-emotSearch=[];
for(EMOT中的var p){
if(emots.hasOwnProperty(p)){
emotSearch.push(p.replace(/[-[{()*+?.\\^$\;]/g,\\$&');
if(p!==p.toLowerCase()){
emots[p.toLowerCase()]=emots[p];
}
}
}
emotSearch=RegExp(emotSearch.join(“|”),“gi”);
功能微笑(str){
return str.replace(emotSearch,函数($0){
var emot=$0.toLowerCase();
if(emot中的emot){
返回“”;
}
返回$0;
});
}
或者在运行检查之前只对输入使用.toLowerCase()?里面的(k in emots){@mowgli是的,没错。它只是从替换函数中取出标志,并将它们转换为正则表达式的标志。转义表达式xp
转义所有特殊的正则表达式字符,尽管您确实只需要转义(
和)
如果你添加了使用其他字符的新表情符号,比如:]
或以后的其他字符,那么最好对输入内容进行转义。好吧;)但我在想……在循环中创建函数不是不对吗?它会被创建(或者被选中,不会被重新创建,我不知道)我每写一个字母guess@mowgli是的,对不起。循环中只有一行。我没有说清楚。函数只需要在同一个范围内。在循环之后就是一个好地方,或者你可以完全放弃函数。只需将escapeRegExp(k)
替换为k.replace(/[-\[\]\/\{\}(\)*+?\\\\\\\\^\$\\\\\\$124;]/g、 “\\$&”)
很有魅力。谢谢“我”只在firefox中使用,因为我看到了很多代码。没有尝试。另一个简短的答案很完美。简单就是好的;)就这样吧。不过,正如你所知道的,你提到的另一个答案需要单独搜索和替换emots的每个属性,这会大大降低效率,并且随着你添加mor,会变得更慢它还使用不必要的冗长正则表达式来转义正则表达式元字符。
var emots = {
':)' : 'smile',
':-)' : 'smile'
// Add the rest of your emoticons...
};
// Build the regex that will be used for searches
var emotSearch = [];
for (var p in emots) {
if (emots.hasOwnProperty(p)) {
emotSearch.push(p.replace(/[-[{()*+?.\\^$|]/g, '\\$&'));
if (p !== p.toLowerCase()) {
emots[p.toLowerCase()] = emots[p];
}
}
}
emotSearch = RegExp(emotSearch.join('|'), 'gi');
function smile(str) {
return str.replace(emotSearch, function ($0) {
var emot = $0.toLowerCase();
if (emot in emots) {
return '<img src="/emoticons/' + emots[emot] + '.gif" title="' + $0 + '" />';
}
return $0;
});
}