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?
\\. 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 \.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With