Search code examples
javascriptregexcase-insensitive

javascript case-insensitive match for part of a string only


I have the following regex -

bannerHtml.match(/href\s*=\s*[\"']{clickurl}(.*)[\"']/);

which matches the following -
href = "{clickurl}

Now, I want the matching of href only to be case-insensitive, but not the entire string. I checked adding i pattern modifier, but it seems to be used for the entire string always -

bannerHtml.match(/href\s*=\s*[\"']{clickurl}(.*)[\"']/i); 

Further details I want all of the following to match -
hREF = "{clickurl}
href = "{clickurl}
HREF = "{clickurl}

But, capital case clickurl part should not match -
href = "{CLICKURL}


Solution

  • You can use:

    /[hH][rR][eE][fF]\s*=\s*[\"']{clickurl}(.*)[\"']/
    

    The part that changed is: [hH][rR][eE][fF], which means:

    Match h or H, followed by r or R, followed by e or E, and followed by f or F.


    If you want to make it generic, you can create a helper function that will receive a text string like abc and will return [aA][bB][cC]. It should be pretty straightforward.