Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert this PHP regex to Javascript?

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.

like image 474
Don Rhummy Avatar asked Dec 13 '25 05:12

Don Rhummy


1 Answers

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+)*$/;
like image 114
Paul Avatar answered Dec 15 '25 18:12

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!