正则表达式只重复我提供的字符?

时间:2014-09-27 21:17:40

标签: javascript regex node.js

好的,我想要做的就是匹配像" lol"和" lollll"和" llllol"和" llllooooollll"和" loool"但不是" pfftlol"或者" lolpfft"等

我目前的代码是

_.each(req.room._settings.automod.cussing.words, function(word)
{
    if(req.message.text.match(new RegExp('\\b'+word.split('').join('+?')+'\\b', 'gi')))
    {
        if(req.user && req.user.cache.automod.cussing === 0)
        {
            req.user.cache.automod.cussing = 1;
            req.write(req.user.name+", Please refrain from cussing in your messages this is your first and only warning next time you will be deleted.");
            req.room.delLastUser(req.user.name, 1);
        }
        else
        {
            req.room.delLastUser(req.user.name, 1);
        }
    }
});

并且不在其中

req.message.text.match(new RegExp('\\b'+word.split('').join('+?')+'\\b', 'gi'))

也可以说req.room._settings.automod.cussing.words是[' lol',' what']因为我不想列出实际的cuss字

和req.message.text是' hah lollll嘿'

这也是通过_.each声明或foreach 我使用的是NodeJS

现在它返回单词,只要它部分匹配

任何人都知道我做错了什么?

2 个答案:

答案 0 :(得分:1)

Ismael Miguel的回答是正确的

/\b(l)+(o)+(l)+\b/

在我的代码中看起来像这样

var reg = '\\b('+cuss.split('').join(')+(')+')+\\b'; req.message.text.match(new RegExp(reg, 'gi'));

答案 1 :(得分:0)

试试这个正则表达式:

/.*\b(l+o+l+)\b.*/

var strings = ["who is lol", "looolll am i", "rofl llllooll", "pfftlol", "lolwho", "anotherlol", "lllllol hehe"];

for(var i = 0; i < strings.length; ++i){
var match = strings[i].match(/.*\b(l+o+l+)\b.*/);
  if (match)
    document.write( match[0] + "<br>") ;
}