Last active
May 20, 2016 22:04
-
-
Save gdibble/73821f9eef2d74084a2c to your computer and use it in GitHub Desktop.
Regular Expresion indexOf for Arrays
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
/** | |
* 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 (str) { | |
for (var i in this) { | |
if (str.toString().match(this[i])) { | |
return i; | |
} | |
} | |
return -1; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I stopped attaching this to the Array construct.
I now use this as a function rewritten such as the alternate below.
The following alternate-form that lets you pass in strings which become
caseInsensitive / globally
searched Regular Expressions:Used such as:
Outputs: 2