3

I tried converting this:

$regex = "/^[0-9]+[0-9\.]*(?<!\.)$/"

to all of these, but none are correct:

var regex = /^(?!\.$)[0-9]+[0-9\.]*/;
var regex = /^(?!.*\.$)[0-9]+[0-9\.]*/;
var regex = /^[0-9]+[0-9\.]*(?!\.$)/;

The PHP regex correctly rejects 1.1a and 1., but the javascript regex's do not.

2
  • 2
    Are you trying to be clever with negative look-aheads? Commented Jun 10, 2013 at 18:28
  • possible. I'm trying to get a string that has only numbers and dots and cannot end in a dot. Commented Jun 10, 2013 at 18:33

1 Answer 1

6

Your PHP Regex may be better written as the following, which matches the same language, but is easier to read and doesn't need to use a negative look-behind:

$regex = "/^\d+(\.\d+)*$/"

It is also easy to translate it directly to a Javascript regex:

var regex = /^\d+(\.\d+)*$/;
Sign up to request clarification or add additional context in comments.

Comments

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.