Regex表示只重复我提供的字符的单词

Regex for a word that repeats only the chars i provide?

本文关键字:字符 单词 表示 Regex      更新时间:2023-09-26

好的,所以我想做的是匹配单词,比如"lol"answers"loll"、"llllol"、"llooooollll"answers"loool",而不是"pfftlol"或"lolpft"等。

我当前的代码是

_.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是['olo','hat'],因为我不想列出实际的脏话

并且req.message.text是"哈哈哈哈嘿"

这也是通过一个.each语句或foreach我正在使用NodeJS

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

有人知道我做错了什么吗?

Ismael Miguel的答案是正确的

/'b(l)+(o)+(l)+'b/

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

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

试试这个正则表达式:

/.*'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>") ;
}