Johann
Johann

Reputation: 29867

Javascript: RegExp not working no matter what my expression is

Ok, I've been using RegExp a number of times but for some reason I cannot get it to work this time. I am trying to test for latitude (0 to +/- 90 degrees). No matter what expression I use, it always returns false. Here's my code:

var regexLatitude = new RegExp("^-?([1-8]?[0-9]\.{1}\d{1,6}$|90\.{1}0{1,6}$)");
var status = regexLatitude.test("89.5");

I also tried without quotes:

var status = regexLatitude.test(89.5);

Any idea?

Upvotes: 1

Views: 151

Answers (1)

SLaks
SLaks

Reputation: 887365

Your \ characters are being parsed by the Javascript string literal.

You need to use a regex literal:

var regexLatitude = /^-?([1-8]?[0-9]\.{1}\d{1,6}$|90\.{1}0{1,6}$)/;

Upvotes: 2

Related Questions