Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return true/false for a matched/not matched regex

I have this regex on Javascript

var myS = "00 - ??:??:?? - a"; var removedTL = myS.match(/^(\d\d) - (\?\?|10|0\d):(\?\?|[0-5]\d):(\?\?|[0-5]\d) - (.*)/); 

and I need to return "false" if myS is not in that format, true otherwise.

I mean :

var myS = "00 - ??:??:?? - a";  // TRUE var myS = "00 - ??:?:?? - a";   // FALSE 

how can I check if the regex has matched the string or not?

like image 616
markzzz Avatar asked Aug 13 '11 19:08

markzzz


People also ask

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.

What does match return in regex?

The Match(String, String) method returns the first substring that matches a regular expression pattern in an input string. For information about the language elements used to build a regular expression pattern, see Regular Expression Language - Quick Reference.

Does re match return true?

But to elaborate, re. match() will return either None , which evaluates to False , or a match object, which will always be True as he said.

Can regex match return null?

match(regexp) finds matches for regexp in the string str . If the regexp has flag g , then it returns an array of all matches as strings, without capturing groups and other details. If there are no matches, no matter if there's flag g or not, null is returned.


1 Answers

The more appropriate function here might be RegExp.test, which explicitly gives you true or false.

console.log(/lolcakes/.test("some string")); // Output: false  console.log(/lolcakes/.test("some lolcakes")); // Output: true 
like image 190
Lightness Races in Orbit Avatar answered Sep 20 '22 09:09

Lightness Races in Orbit