Created
April 24, 2017 02:09
-
-
Save reporter123/187a412821f829e1a818fd3bf2996cba to your computer and use it in GitHub Desktop.
Adds a regEx based indexOf function to the Array prototype.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//install helper funtion not found in javascript | |
/** * Regular Expresion IndexOf for Arrays | |
* This little addition to the Array prototype will iterate over array | |
* and return the index of the first element which matches the provided | |
* regular expresion. | |
* Note: This will not match on objects. | |
* @param {RegEx} rx The regular expression to test with. E.g. /-ba/gim | |
* @return {Numeric} -1 means not found */ | |
if (typeof Array.prototype.reIndexOf === 'undefined') { | |
Array.prototype.reIndexOf = function (rx) { | |
for (var i in this) { | |
if (this[i].toString().match(rx)) { | |
return i; | |
} | |
} | |
return -1; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment