I have recently been spending countless hours tinkering with regular expressions (for a project that I’ll blog within the next few days, I hope). ColdFusion has great regular expression support, but the REFind() and REFindNoCase() functions annoy me no end. I like that I can opt for a position and length to be returned instead of just a position, but why does the array just contain the first instance? Almost any regex code I write needs to include looping code to call REFind() repeatedly shifting the position each time. That’s just plain silly, especially when the returned values are arrays (heck, why even bother using arrays if all that is ever returned is a single element?).
So, I wrote my own REFindAll() function. It does exactly what REFind() does (taking the same first two parameters) and returns he same array that REFind() does, but that this version returns all instances (one array element per instance). Enjoy.
And for the sake of completeness, here’s REFindNoCaseAll(), the case-insensitive version of the UDF.