Reputation: 660
I need to check whether a string contains other than the specified words/sentence (javascript), it will return true if:
ANOTHER CMD
["8809 8805", "8806 8807"]
(the numbers are examples I should be able to test the string for any array of numbers)Thank you!
Upvotes: 0
Views: 90
Reputation: 177860
Yes you can replace all not in the array
const arr = ["ANOTHER CMD","8809 8805", "8809 8805"]
const okContent = str => {
arr.forEach(entry => str = str.replaceAll(entry,""))
return str.trim()==="";
};
console.log(okContent('Has other stuff than ANOTHER CMD and 8809 8805'))
console.log(okContent('8809 8805 ANOTHER CMD 8809 8805'))
Upvotes: 1
Reputation: 491
you can try regex!
use your array of strings as the '|' separated regex value
and check the specified string in the given line. if it presents negate the output.
const regex = /(ANOTHER CMD|8809 8805|8806 8807)/i
console.log(!regex.test('Should not contain word ANOTHER CMD'))
console.log(regex.test('Should contain word ANOTHER CMD'))
Upvotes: 0
Reputation: 660
I don't know if it's the correct way of doing it but this worked for me:
replace
)trim
method)Upvotes: 0