Skip to content

Instantly share code, notes, and snippets.

@reporter123
Created April 24, 2017 02:09
Show Gist options
  • Save reporter123/187a412821f829e1a818fd3bf2996cba to your computer and use it in GitHub Desktop.
Save reporter123/187a412821f829e1a818fd3bf2996cba to your computer and use it in GitHub Desktop.
Adds a regEx based indexOf function to the Array prototype.
//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