Javascript is not case sensitive for part of string only

I have the following regex -

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

which corresponds to the following -
href = "{clickurl}

Now I want the href match to only be case insensitive, but not the whole string. I checked the addition of the template i modifier, but it seems to be used for the whole line always -

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

Additional Information I want all of the following to match -
href = "{clickurl}
href = "{clickurl}
href = "{clickurl}

But part of clickurl capital clickurl not match -
href = "{clickurl}

+4
source share
3 answers

You can use:

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

Modified part: [hH][rR][eE][fF] , which means:

Match h or h , then r or r , then e or e , then f or f .


If you want to make it general, you can create a helper function that takes a text string like abc and returns [aA][bB][cC] . It should be pretty simple.

+5
source

You cannot make it partially case sensitive, but you can always be specific:

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

An alternative to this is to reject false matches using a secondary regular expression.

As a note, it is not necessary to avoid quotation marks " because the delimiter / is a delimiter.

+2
source

First of all, I have to say that this is a very good question. I thought of 2 solutions to your problem:

  • make all href lines lowercase:

    bannerHtml.replace(/href/ig,"href")

  • First of all, I wrapped {clickurl} with parentheses for future reference: ({clickurl}) . Then I matched the entire string, which is not sensitive to strings, to see if it matches the pattern. Finally, I checked the correspondence of the string {clickurl} , which is stored in result[1] , and see if it is in the exact case.

     var re=/href\s*=\s*[\"']({clickurl})(.*)[\"']/i; var result = re.exec(bannerHtml); if(result && result[1]=="{clickurl}"){ //Match! } 

I know its not a very regular solution, but I, that is the best that I could think of. Good luck.

0
source

All Articles