2
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.

3 Answers 3

4

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'
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Outstanding approach.
@Wasim Thanks, i just re-edited it, i changed the [1-9] character sets to \d digits because otherwise 20h5M wouldn't match.
i do understand that, i understand regex but can't creat it due to lack of practice with regex. Thanks again
1

The plus means "at least one" - remove it. You may also want to use [0-9] for all digits, but that is just a guess.

patt=/^[1-9]d/i;

3 Comments

correct, if str = "23h34M" then it should also return true. is that possible
this comment makes no sense, there is no correlation between it return false for 33d4M but true for 23h34M. to have a regex work you need a pattern...what's the pattern?
pattern is [1-9]d or [1-9][1-9]h , basically d for days and h for hours. i need return true if days are less than 10 or few hours left
1

i think something like this would work:

patt=/^[1-9][dh]/i

4 Comments

can you put some sample cases. how much is "a few hours?"
hours obviously will not be greater than 23
patt=/^[1-9]d|h/i is working for me, but i think it should not work. Why its working. it should return false on str = "23h" but it true, WHY?
str = "10d 10h 7m"; here what pattern should return false because days are greater than or equal to 10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.