How can one check a url if it contains a specific word, but also check to see if it doesn’t contain a word? Its better if I try and display an example:
Do something if the word ‘pan‘ is in the URL, however do NOT do anything when the word ‘panini‘ is in the url:
if (/pan/.test(self.location.href) && (/panini/.test(self.location.href) == null) {...}
The above first part works fine, but with the second panini part added it of course will not work, anyone have any ideas?
testreturns a bool, so just use the!operator:This could be simplified to use
indexOf(should be faster):Additionally, a regex testing for a word can use a word boundary,
\b: