I'm trying to find exact word in text user is sending, but obviously, when I'm trying to use message.content.includes(), it's also looking for parts of the word in text which I don't need! Any way to search by full words only?
Few examples: **TexT**, HeLlO, etc.
CodePudding user response:
Yes, you can use a regex like this, assuming wordToFind holds the word you are searching for:
// Create regex from word with \b at each end which
// means "word boundary", and the 'i' option means
// case-insensitive
const wordSearch = new RegExp(`\b${wordToFind}\b`, 'i');
// Use the regular expression to test the content:
const hasWord = wordSearch.test(message.content);
// will be true if whole word is found
CodePudding user response:
Put your content in to an array, splitting words if it's a piece of text. Array.includes will match whole words.
So either [content].includes(word) or content.split(' ').includes(word)
