[英]How to use if condition in Javascript RegEx?
I am trying to add some rules to Imagus Firefox Extension . 我正在尝试向Imagus Firefox Extension添加一些规则。 I want to capture image parameter from Google Image Search and if it contains the string
th_
remove it and redirect. 我想从Google Image Search中捕获图像参数,如果它包含字符串
th_
请将其删除并重定向。 Otherwise just redirect. 否则,只需重定向即可。
This is my RegEx : 这是我的RegEx :
/^(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(?:th_)(.*)&imgrefurl=.*/gm
It works fine for URL's which contain string th_
but for other links it breaks. 对于包含字符串
th_
URL来说,它工作正常,但对于其他链接,它会中断。
Here's the link to my work https://regexr.com/3omf5 Have a look and help. 这是我的工作的链接https://regexr.com/3omf5寻求帮助。
PS: Please note there are two links in the example. PS:请注意,示例中有两个链接。
I found the answer after a fight. 经过一番斗争,我找到了答案。 And the regex works fine in the Extension.
正则表达式在扩展中可以正常工作。
Ans: 答:
^(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(%2Fimages(?:[\d]{1,9})?%2F)(th_)?(.*)&imgrefurl=.*
Here is th link with answer: 这是答案的链接:
https://regexr.com/3omfh https://regexr.com/3omfh
在(?:th_)
之后添加*
,例如:
^(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(?:th_)*(.*)&imgrefurl=.*
^(?:(?:(?:images|encrypted)\.)?google\.[^/]+/(?:imgres\?(?:[^&]+&)*?imgurl=)(.*)(?:th_)(.*)&imgrefurl=.*)|(.+)
Matches your urls with th_ and replaces it or takes the whole url with the additional |(.+)
(+ ^(?: ... )
around your regex). 将您的网址与th_匹配并替换,或将整个网址与正则表达式周围的其他
|(.+)
(+ ^(?: ... )
)一起使用。 You need to replace it with $1$2$3
then 您需要将其替换为
$1$2$3
然后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.