Javascript profanity match NOT replace

前端 未结 3 1332
天涯浪人
天涯浪人 2021-01-28 10:46

I am building a very basic profanity filter that I only want to apply on some fields on my application (fullName, userDescription) on the serverside

3条回答
  •  失恋的感觉
    2021-01-28 11:19

    How about with fixed regexp:

    check = new Regexp('(^|\b)'+badWords.join('|')+'($|\b)', 'gi');
    
    check.test('ass') // true
    check.test('suckass') // false
    check.test('mass of whore') // true
    check.test('massive') // false
    check.test('slut is massive') // true
    

    I'm using \b match here to match for word boundry (and start or end of whole string).

提交回复
热议问题