0

Does anyone know why jsHint says this regular expression has a "Bad Escapement"?

var regexp = new RegExp('^http(s)?:\/\/([a-z]+\.)?(' + this.opts.domain + ')', 'ig');

It's complaining about the escaped period \.

The regex still works without escaping the period. My goal is to find if a URL contains a given domain name, http://rubular.com/r/5U7kVjhleu

Michael Turnwall
  • 649
  • 7
  • 21

1 Answers1

1

If you construct a regex from a string, you need to double the backslashes (and you don't need to escape the slashes):

var regexp = new RegExp('^http(s)?://([a-z]+\\.)?(' + this.opts.domain + ')', 'ig');
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thanks for the answer! This is the first I heard of having to do a double backslash in a regex string to escape characters. I've been doing it wrong all these years lol. – Michael Turnwall Feb 11 '13 at 23:20