javascript - Regex expression is not working? -
I am trying to create a regex expression with this requirement.
Requirement: Maximum Length - 5 (inc decimal point if decimal number is) Decimal Accession - Maximum 2 digits (if it is a decimal number). Number - no need to be a decimal number (not mandatory) Code: The output should look like: 12345.52 - & gt; It should be returned to 123456.52 - & gt; false . I found out 12.45 - & gt; truth . Correct One (Length 5, Precision 2) 12345 - & gt; truth . I am hoping to make a Rizax expression, I satisfy all the above situations. Reference: You can use the regex given below. Explanation: < Code> & lt; Script & gt; Function myFunction () {var regexp = / ^ (????? \\.? $) \ D {0,5} (\. \ D {0,2})? $ /; Var num = 12345.52; // I will test indifernt methods var n = regexp.test (num) document.getElementById ("Demo") here. InnerHTML = n; // returns true or false} & lt; / Script & gt;
false because length is 8 inc dot but this return
true
d {0,5} looking for decimal before
^ (? =. {1,5} $) \ D + (?: \. \ D {1,2})? $
^ assures That we are in the beginning
(? =. {1,5} $) indicates that the length should be from 1 to 5.
\ d + one or more digits.
(?: \. \ D {1,2})? After the decimal point, the optional decimal part should be one or two with acceptable digits.
$ indicates that we are at the end of the line
Comments
Post a Comment