Javascript Regex check if URL contains one word AND does NOT contain another -


How can a URL check if there is a specific word in it, but also see if there are no words in it? If I try to try and display an example then it would be better:

Do something if the ' pan ' is in the URL, then the word ' Panini 'url has:

  if (/pan/.test(self.location.href) & amp; (/ panini /.test(self.location.href) = = Null) {...}   

The first part above works fine, but with the other part, will there be no work in it, does anyone have any ideas?

returns a boole, so just ! Use operator:

  if (/pan/.test(self.location.href) & amp; (/ Panini / .test (self.location.href)) { ...}   

It can be simplified to use (should be fast):

  if (self.location.href. IndexOf ("pan") & gt; -1 & amp; amp; amp; & amp; amp; self.location .href.indexOf ("panini") == -1) {...}   < P> Additionally, you can use a regex test term limit for one word,  \ b :  
  / \ bpan \ b / .test (self .locati On.href) // "Pan" will match "Pan and food", but "Panini" will not be    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -