javascript - Enforcing that a given RegExp is case insensitive -


i have javascript function receives regular expression 1 of arguments. make sure regexp has (case insensitive) modifier, , if not, add it.

var caseinsensitivematch = function (rx) {   // todo: verify rx has modifier. add if doesn't   return rx.exec('my text'); }  // both should match: caseinsensitivematch(/my text/); caseinsensitivematch(/my text/i); 

what's elegant way this?

if want preserve flags , add case-insensitivity:

function caseinsensitivematch(rx, text) {   var flags = 'i';   if (rx.multiline) flags += 'm';   if (rx.global) flags += 'g';   return (new regexp(rx.source, flags)).test(text); } 

Comments