There is no built-in tool in Javascript RegExp; without changing your expression. The closest you can get is source , which will simply return the whole expression as a string.
Since you know that you are an expression, this is a series | OR | OR s, you can capture the groups to find out which group was mapped, and combine this with .source to find out the contents of this group:
var exp = /(horse)|(caMel)|(TORTOISe)/i; var result = exp.exec("Camel"); var match = function(){ for(var i = 1; i < result.length; i++){ if(result[i]){ return exp.source.match(new RegExp('(?:[^(]*\\((?!\\?\\:)){' + i + '}([^)]*)'))[1]; } } }();
It is also very easy (albeit somewhat impractical) to write a RegExp engine from scratch, you could technically add this functionality. This will be much slower than using a real RegExp object, since the whole engine must be interpreted at run time. However, it could return the exact matched part of the expression for any regular expression and not be limited to what consists of a series of | OR | OR s.
However, the best way to solve your problem is probably not to use a loop or regular expression at all, but instead create an object in which you use the canonical form for the key:
var matches = { 'horse': 'horse', 'camel': 'caMel', 'tortoise': 'TORTOISe' }; // Test "Camel" matches['Camel'.toLowerCase()]; // "caMel"
source share