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?

--------------Solutions-------------

The match method will return null if there is no match.

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

Use a double logical NOT operator.

return !!removedTL;

This will convert to true/false depending on if matches are found.

No matches gives you null, which is converted to false.

One or more matches gives you an Array, which is converted to true.



As an alternative, you can use .test() instead of .match().

/^(\d\d) - (\?\?|10|0\d):(\?\?|[0-5]\d):(\?\?|[0-5]\d) - (.*)/.test( myS );

...which gives you a boolean result directly.

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

removedTL will hold a boolean value true if matched, false if not matched

Category:javascript Time:2011-08-13 Views:1

Related post

  • javascript Confirm replacement with return true/false 2011-08-10

    Since jquery UI dialog does not support returning true/false, I need some other way to replace a javascript confirm. It has to return true/false, so that my validation processes in javascript will run: var where_to_coupon = confirm(pm_info_msg_013);

  • Is there a Java convention for returning true, false, or unknown? 2011-10-07

    I am writing a method which will return if something is true or not. But if the resources needed to test the condition are not available, it will not be able to return true or false. Earlier, I was just making it a method which returns a boolean. But

  • checkbox returning "true,false" from formcollection instead of selected value 2012-03-10

    im having multiple checkboxes in my aspx page using HtmlHelper.CheckBox and when the form is submitted using ajax as below in js file var input = $(':input'); $.ajax({ type: 'POST', url: "/Home/Post", data: input, dataType: 'json', success: function

  • Find text in string and return TRUE/FALSE 2012-04-09

    I want to find text in a string, and return TRUE/FALSE. As far as i can tell, FIND and SEARCH will return an #VALUE! error if the text is not found, which means that I have to use a two-step approach, something like ISERROR(SEARCH(...)). That seems u

  • how to return true/false from nested jquery callback functions 2013-08-09

    I am trying to validate elements inside a javascript function which contains two jQuery callback loops. Based on the conditions I want to return true/false from the inner jQuery loop and that should be sent back to the calling method of javascript. I

  • Adapting methods which return true/false 2010-04-04

    What's the best practise when adapting C-style functions which return a true/false to Java? Here's a simple method to illustrate where the problem lies. public static boolean fileNameEndsWithExtension( String filename, String fileExtension) { return

  • Return true/false on searching multidimensional array 2010-11-05

    I have the following multidimensional $array: Array ( [0] => Array ( [domain] => example.tld [type] => 2 ) [1] => Array ( [domain] => other.tld [type] => 2 ) [2] => Array ( [domain] => blaah.tld [type] => 2 ) ) I simply wan

  • python regular expressions return true/false 2011-07-04

    Using python regular expressions how can you get a true/false returned? All python returns is <_sre.SRE_Match object at ...> --------------Solutions------------- Match objects are always true, and None is returned if there is no match. Just tes

  • How can I return true/false from an animation queue thats inside a click() function? 2009-03-07

    This seems so tricky to me that I think I am overlooking something simple here. Can you help me find it? Basically, the situation is this (variables where the name is not important are named with a capital letter) : $('a').filter('somecriteria').each

  • Why does my DataTable always returns "true/false" but never a string? 2009-07-12

    I'm trying to use a DataTable, filled by a MySqlDataAdapter, which contains a list of comments for a blog entry. For some reasons, if the field "anonymous" is set to "1", the username field is empty and should be replaced with a specified string. The

  • How to return TRUE|FALSE from class in PHP 2009-08-24

    I was wondering why my php program is not returning the correct TRUE FALSE value when the class is included elsewhere it goes something like this source: signup.php class signup { function buildProfile() { if($logic){ $this->success = TRUE; }else{

  • SQL query return True/False 2009-11-10

    I am working with an existing architecture which populates fields on a form with things from the database. The form is entirely database driven and is done in PHP and MySQL. The problem is that the database table that holds the form elements has a fi

  • jQuery get function to return true/false 2010-06-23

    $(document).ready(function(){ //global vars var name = $("#username"); var email = $("#email"); function usernameExists() { $.get("register.php",{ check: 1, username: name.val(), email: email.val() } ,function(m) { if(m==1) { return false; } else { r

  • jQuery.ajax / CodeIgniter (PHP) : Returning TRUE/FALSE in PHP, how can I get jQuery to see this? 2010-07-13

    I've built a function for checking a username. I'm using it as a callback for form validation in CodeIgniter, and it works nicely. However, I'd like to also use it with AJAX to check on the fly if the user has JS enabled. So my controller has this: f

  • Return true/false to javascript from php file 2011-01-07

    I'm using $.post method to make ajax calls. I have a script (php) that checks for user existing in database and returns (echo's) 1 if exist and 0 if not. Is it possible to return true and false so javascript recognize it as boolean ? --------------So

  • How do I express that I want to do something "if a function returns true/false" 2011-03-28

    I am making a dictionary program. Before adding a word to the dictionary, the AddWord function calls the SearchForWord function, if the search functions discovers that the word passed to it is already in the dictionary it returns true. In the add fun

  • PHP - Validation function to return true|false, AND a message if false 2011-04-23

    I have a validation function which returns either true or false. However, I want it to provide info as to what the problem is, when there is one. Let's say the function is like this: function is_valid($val) { $result = true; if( rule_1_not_met ) $res

  • Execute 3 functions that return true/false and if all return true do something or else, no short circuiting 2011-09-28

    I have 3 JS functions a() b() c() I want to execute all 3 and also check if all 3 return true then I want to call function yeah() or else call function boo() I can use && but it will short circuit and may not execute all 3 functions if first

  • Why multiple Android SoundPool.unload calls return true/false depending on order? 2012-01-21

    According to Android documentation, SoundPool.unload returns true if just unloaded, false if previously unloaded I create the SoundPool, load 5 sounds and store related 5 sound IDs. Then before leaving the Activity I invoke unload for each of those I

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.431 (s). 13 q(s)