admin管理员组文章数量:1387388
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}
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}
3 Answers
Reset to default 6You 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.
You can't make it partially case-sensitive, but you can always be specific:
bannerHtml.match(/[hH][rR][eE][fF]\s*=\s*["']{clickurl}(.*)["']/);
The alternative to this is to discard false matches using a secondary regular expression.
As a note, it's not required to escape quote characters "
as only the slash /
is the delimiter.
First of all I must say that's a very good question. I thought of 2 solutions to your problem:
make all
href
strings in lowercase:bannerHtml.replace(/href/ig,"href")
First of all I wrapped {clickurl} with parentheses for later use:
({clickurl})
. Then, I matched the whole case insensitive string to see if it matches the pattern. Lastly, I checked the{clickurl}
string match which is stored inresult[1]
and see if its 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 very regex solution but I that's the best I could think about. Good luck.
本文标签: regexjavascript caseinsensitive match for part of a string onlyStack Overflow
版权声明:本文标题:regex - javascript case-insensitive match for part of a string only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744519622a2610376.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论