Design & Download
Your JS Library
in Seconds!!!

Snippet: isRegExpMatch() - Match RegExp Against Start of String or Entire String

Written by Christopher West (cwest) on August 24, 2016.
Determine whether or not a RegExp matches an entire string (or the start of a string).
function isRegExpMatch(rgx, opt_str, opt_onlyCheckStart) {
  rgx = RegExp(rgx.source + '|([\\S\\s])', (rgx + '').replace(/[\s\S]+\/|g/g, '') + 'g');
  function f(str, opt_checkStartOnly) {
    rgx.lastIndex = undefined;
    opt_checkStartOnly = 1 in arguments ? opt_checkStartOnly : opt_onlyCheckStart;
    var isMatch = false, match, keepGoing = 1;
    while ((match = rgx.exec(str)) && keepGoing) {
      isMatch = slice(match, -1)[0] == undefined;
      keepGoing = isMatch && !opt_checkStartOnly;
    }
    return isMatch;
  }
  return opt_str == undefined ? f : f(opt_str, opt_onlyCheckStart);
}

isRegExpMatch(...) API Documentation

Description

Determine whether or not a RegExp matches an entire string (or the start of a string).

Parameters

  1. rgx {RegExp}:
    The regular expression to be used.
  2. opt_str {string}:
    Optional. If specified, rgx will be matched against the string to see if the string regular expression matches from the beginning. If not specified, a function will be returned that will be used to match against rgx whenever it is called.
  3. opt_onlyCheckStart {boolean}:
    Optional. Defaults to false. If true, rgx must only match the beginning of opt_str. If false, rgx must match the entire string opt_str.

Returns

If opt_str is given and is not null or undefined, a boolean will be returned. Ifopt_onlyCheckStart is true-ish the value returned will indicate if rgx simply matched from the beginning of opt_str. If opt_onlyCheckStart is false-ish, the value returned will indicate if rgx matched all of opt_str. If opt_str is not given or null or undefined a function to match against rgx will be returned:
  • Argument 1 - str {string}:
    The string to match against rgx.
  • Argument 2 - opt_checkStartOnly {boolean}:
    Optional. Defaults to opt_onlyCheckStart. If true, rgx will only need to match from the start of str. If false, rgx will have to match the entire string str.
  • Returns {boolean}:
    true is returned if rgx matches str as specified by opt_checkStartOnly. Otherwise false is returned.
Returns an array with two values: the string found before target and the string found after target. If target was not found or the specified occurrence (opt_occurrence) of target was not found, [null, null] will be returned.

Examples

var isDigits = YourJS.isRegExpMatch(/\d/),
    isHexDigits = YourJS.isRegExpMatch(/[\dA-F]/i),
    isDigit = YourJS.isRegExpMatch(/\d/, null, true),
    isHexDigit = YourJS.isRegExpMatch(/[\dA-F]/, undefined, true);

/*** Test Base isRegExpMatch() ***/
console.log(YourJS.isRegExpMatch(/\w/, 'Howdy'));         // -> true
console.log(YourJS.isRegExpMatch(/\w/, 'Howdy!'));        // -> false
console.log(YourJS.isRegExpMatch(/\w/, 'Howdy!', true));  // -> true
console.log(YourJS.isRegExpMatch(/\w/, '*Howdy!', true)); // -> false

/*** Test isDigits(): Defaults to checking all characters ***/
console.log(isDigits(''));                                // -> false
console.log(isDigits('3'));                               // -> true
console.log(isDigits('35'));                              // -> true
console.log(isDigits('3.5'));                             // -> false
console.log(isDigits('3.5', true));                       // -> true

/*** Test isHexDigits(): Defaults to checking all characters ***/
console.log(isHexDigits(''));                             // -> false
console.log(isHexDigits('D'));                            // -> true
console.log(isHexDigits('f2'));                           // -> true
console.log(isHexDigits('ej'));                           // -> false
console.log(isHexDigits('ej', true));                     // -> true

/*** Test isDigit(): Defaults to only checking the beginning ***/
console.log(isDigit(''));                                // -> false
console.log(isDigit('3'));                               // -> true
console.log(isDigit('35'));                              // -> true
console.log(isDigit('3.5'));                             // -> true
console.log(isDigit('3.5', false));                       // -> false

/*** Test isHexDigit(): Defaults to only checking the beginning ***/
console.log(isHexDigit(''));                             // -> false
console.log(isHexDigit('D'));                            // -> true
console.log(isHexDigit('f2'));                           // -> true
console.log(isHexDigit('ej'));                           // -> true
console.log(isHexDigit('ej', false));                    // -> false