Belldandu
Belldandu

Reputation: 2392

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

Ok so what im trying to do is match words like "lol" and "lollll" and "llllol" and "llllooooollll" and "loool" but not "pfftlol" or "lolpfft" etc.

my current code is

_.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);
        }
    }
});

and out of it

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

also lets say that req.room._settings.automod.cussing.words is ['lol','what'] since i dont want to list actual cuss words

and req.message.text is 'hah lollll hey'

also this is going through an _.each statement or foreach I'm using NodeJS

right now it returns the word as long as it partially matches

anyone know what I'm doing wrong?

Upvotes: 0

Views: 111

Answers (2)

Belldandu
Belldandu

Reputation: 2392

The answer from Ismael Miguel is correct

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

which looks like this in my code

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

Upvotes: 1

Johan Karlsson
Johan Karlsson

Reputation: 6476

Try this regex:

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

Upvotes: 0

Related Questions