1

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?

1 Answer 1

2

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}$)/;
Sign up to request clarification or add additional context in comments.

1 Comment

mellamokb: You're right. I need a double slash. SLaks solution is also good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.