0

I have a variable like this

var time = "12h 55m";

I am only allowed to use H,h,M,m characters in the string. If i have something like this

var time = "12hk 55m";

then it should produce an error. how can I validate this using regex expression.'

looking for something like this

if (stringToTest.match(/^[a-zA-Z0-9]*$/))

8
  • so 12m 55h is also valid? Commented Apr 19, 2017 at 6:14
  • yep it is valid Commented Apr 19, 2017 at 6:15
  • How about 65h 097h? Commented Apr 19, 2017 at 6:16
  • 2
    In that case "12h 55m".match(/^\d{1,2}[hm]\s+\d{1,2}[hm]$/g) Commented Apr 19, 2017 at 6:17
  • @Kaivosukeltaja only one h or m allowed Commented Apr 19, 2017 at 6:17

4 Answers 4

2

Try

/^\d{1,2}[hm]\s+\d{1,2}[hm]$/i 

It matches 2 digits followed by either h or m, followed by one or more space, followed by 2 digits followed by either h or m

Following will match

"12h 55m".match(/^\d{1,2}[hm]\s+\d{1,2}[hm]$/i) "12m 55h".match(/^\d{1,2}[hm]\s+\d{1,2}[hm]$/i) "2m 55h".match(/^\d{1,2}[hm]\s+\d{1,2}[hm]$/i) "12m 5h".match(/^\d{1,2}[hm]\s+\d{1,2}[hm]$/i) 

These will not

"122h 555m".match(/^\d{1,2}[hm]\s+\d{1,2}[hm]$/i) 
Sign up to request clarification or add additional context in comments.

9 Comments

hours can comes with 3 integers or 2
This matchees 12h 55h. In the comments above, the OP says they are allowed to use only one h
@sachilaranawaka In that case replace {1,2} with {2,3}
/^\d{1,2}h\s+\d{1,2}m$/gi is probably better.
@jrook in that case it will be "12h 55m".match(/^(\d{1,2}h\s+\d{1,2}m)|(\d{1,2}m\s+\d{1,2}h)$/g)
|
0

The reg in regex stands for regular and your data seems to have a possibility to be irregular. I'd recommend to do the check differently but since you're looking for a regex solution:

/^(\d{2,3}h\s+\d{1,2}m)|(\d{1,2}m\s+\d{2,3}h)$/gi

This will match h and m in either order but will reject if either is in the string twice.

Comments

0

You could use following regex ^\d{1,2}[hmHM]\s\d{1,2}[hmHM]:

  • ^ asserts position at start of the string
  • \d matches a digit (equal to [0-9])
  • {1,2} Quantifier — Matches between 1 and 2 times, as many times as possible, giving back as needed
  • [hmHM] matches a single character in the list hmHM (case sensitive)
  • \s matches any whitespace character
  • \d{1,2}[hmHM] as described above
  • \g modifier: global. All matches (don't return after first match)

See following snippet to test it:

var regex = /^\d{1,2}[hmHM]\s\d{1,2}[hmHM]/g; function check(par){ console.log(par.value + " match: " + regex.test(par.value)); }
Insert a text in the input <input type="text" id="test" value="" onchange="javascript:check(this)">

Comments

0

The accepted answer apparently satisfies the OP. But I noticed that in this comment the OP says that the character symbols should not be repeated. For example, 12h 12h should be invalid but all answers match this. I don't think this can be done using only regex. So here is an alternative solution:

function timeParser(timeStr) { var acChars = ['h', 'H', 'm', 'M']; if ((timeStr.match(/\s/g) || []).length !== 1) return false; var tokens = timeStr.split(' '); for (var token of tokens) { var rx = new RegExp("\\d{1,3}[" + acChars.join("") + "]", "g"); if (!token.match(rx) || token.match(rx).length !== 1 || token !== token.match(rx)[0]) return false; var tc = token.charAt(token.length - 1); acChars.splice(acChars.indexOf(tc), 1); } return true; } var timearr = ["12h 12h", "1m1h 515M", "12hk 55m", "H 12m", "m 12H", "12H 11m", "00m 001h", "20M 1"]; for (var tim of timearr) console.log(timeParser(tim));

and 12h 12h is not matched.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.