我不明白为什么这不起作用,但是如果你注释掉countGrin函数,它的效果非常好:
var DetectEmoticons = {
countHappy: function(data) {
var pattern = new RegExp("[:=]-?[)|\\]]|[(|\\[]-?[:=]", "g");
var count = (data.match(pattern) || []).length;
return count;
}
countGrin: function(data) {
var pattern = new RegExp("[:=]-?[D]", "g");
var count = (data.match(pattern) || []).length;
return count;
}
}
alert(DetectEmoticons.countHappy("=) :) :] :[ ]="));
alert(DetectEmoticons.countGrin(":D =D"));
这是jsfiddle:http://jsfiddle.net/9BTAb/
答案 0 :(得分:4)
也许你在第一个函数SEE FIDDLE
之后缺少,
var DetectEmoticons = {
countHappy: function(data) {
var pattern = new RegExp("[:=]-?[)|\\]]|[(|\\[]-?[:=]", "g");
var count = (data.match(pattern) || []).length;
return count;
}, //put a comma here
countGrin: function(data) {
var pattern = new RegExp("[:=]-?[D]", "g");
var count = (data.match(pattern) || []).length;
return count;
}
}