Search code examples
javascriptregexpattern-matchingmatchdigit

Javascript RegEx Exactly one Digit Pattern


str = "33d4m"; //d for days and h for hours and m for min
patt=/^[1-9]+d/i;
result=patt.test(str);
document.write("Returned value: " +  result);

I want result return true if and only if there is one digit before d, i.e;less than 10days remaining or a few hours remaining like i want return true also on

str = "23h5m"  

if two digit before d then return false
if two digit before h then return true.
Where i am going wrong.


Solution

  • You could try this:

    patt=/^\d{1,2}h|^\dd/i
    

    It means:

       Match 1 or 2 digits followed by the literal 'h' 
    OR match a single digit followed by the literal 'd'