Michael Turnwall
Michael Turnwall

Reputation: 671

jshint error with regular expression

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

Upvotes: 0

Views: 269

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336418

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');

Upvotes: 1

Related Questions