Here is a method I have created to check if a user-input term matches any blacklisted terms:
static checkAgainstBlacklist(blacklistTerms, term) {
return blacklistTerms.some(word =>
(new RegExp(`\\b${word}\\b`, 'i')).test(term)
);
}
I'm encountering an issue with words containing special characters:
it('should return true if sentence contains blacklisted term',
inject([BlacklistService], () => {
const blacklistTerms = [
'scat',
'spic',
'forbanna',
'olla',
'satan',
'götverenlerden',
'你它马的',
'幼児性愛者',
];
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'scat')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'scat-website')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'spic')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'website-spic')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'forbanna')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'olla')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'satan-website')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'götverenlerden')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, '你它马的')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, '幼児性愛者')).toEqual(true);
})
);
While most tests pass, these three are failing:
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, 'götverenlerden')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, '你它马的')).toEqual(true);
expect(BlacklistService.checkAgainstBlacklist(blacklistTerms, '幼児性愛者')).toEqual(true);
What adjustments can I make to my regex command to handle these terms correctly?