Regular Expression in Array.indexOf()

A few minutes ago I found I needed to find the first index of an element in an array using RegEx. Unfortunately, such a tool is not in the JavaScript toolbox so without further delay, here’s what I came up with.

 * 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;

// Try it out

// Array of strings
// Should return 3
var test = ['foo', '-bar', 'droopy', 'dog'];
console.log( 'array of strings:', test.reIndexOf(/og/) );

// Array with numbers
// Should return 3
var test = ['foo', '-bar', '1', 2];
console.log( 'array with numbers:', test.reIndexOf(/2/) );

// Array with objects
// Should return 3
var test = ['foo', { 'test':'one' }, { 'test':2 }, 2];
console.log( 'array of obj:', test.reIndexOf(/2/) );

Using RegEx and Java to Split a String in Coldfusion

I was looking for a great way to split a string using a regex expression. Ben Nadel had a good post where he’s creating an RESplit() method. This was a little more than I wanted to go into, and after some more searching I found the solution was to use Java (usually is the solution ;-) as shown by Raymond Camden. So without further discussion, here’s what you need:

// Splitting a query column value by linefeeds
// You could just as easily do this with any RegEx
ItemsArray = myQuery.Notes[1].split( chr(13) );

Regular Expression to match most International phone numbers in US notation

So here’s a little RegEx I’ve whipped up that will match all of the following phone number notation styles:


EDIT: Fellow Google Pluser Bradley Moore took a crack at my expression above and cleaned it up a bit. Here’s the update:

^\+?\d{0,3}[ -]?\(?\d{3}\)?[ -]*\d{3}[ -]*\d{4}$


555 555-5555






+12 555-555-5555

+1 (555) 555-5555

(555) 555-5555