我正在尝试编写一个正则表达式,它将检查 URL 是否包含某些单词并排除另一个单词。
原因是我正在尝试跟踪通过我的网站的流量,并且我不想计算点击“谢谢”页面的任何人。
例如:
-
http://www.mywebsite.com/register-now/
- MATCH
-
http://www.mywebsite.com/contact-us/
- MATCH
-
http://www.mywebsite.com/register-now/thank-you
- 不匹配
-
http://www.mywebsite.com/contact-us/thank-you
- 不匹配
-
http://www.mywebsite.com/thank-you
- 不匹配
我有2个字(register-now
and contact-us
) 这些必须位于 URL 中。但是我必须确保 1 个字(thank-you
) 也不在 URL 中。
我尝试使用否定前瞻来检查 URL 是否不包含谢谢,但它不起作用:
"^(?!.*\/thank\-you+)\/(contact\-us|register\-now)\/.*"
在单个正则表达式中,您可以使用负向前瞻:
^(?!.*\/thank-you(?:\/|$))(?:.*\/)?(?:contact-us|register-now)\/
正则表达式演示 https://regex101.com/r/2JjwPA/5
-
(?!.*\/thank-you(?:\/|$))
是负向前瞻,如果 URL 有,则匹配失败/thanks-you
or /thank-you/
.
- Use
MULTILINE
模式,如果您的文本每行包含多个 URL。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)