Skip to main content
2 of 2
edited tags
nnnnnn
  • 150.4k
  • 30
  • 210
  • 247

Regex inside `if` check

I'm trying to use regex inside if. Strange, but it doesn't work. How it may be fixed?

It works:

var lang = 'lang-js'; if (lang == 'lang-js') { alert('ok'); } 

It works too (just for testing purposes):

var lang = 'lang-js'; if (lang == 'lang-' + 'js') { alert('ok'); } 

But this one doesn't work:

var lang = 'lang-js'; if (lang == 'lang-' + /[a-z]/) { alert('not ok'); } 
john c. j.
  • 1.3k
  • 5
  • 41
  • 94