简体   繁体   English

不包含正则表达式中包含字符串的URL

[英]Excluding URLS that contain a string in Regex

I am using regex to add a survey to pages and I want to include it on all pages except payment and signin pages. 我正在使用正则表达式将调查添加到页面,并且希望将其包括在除付款和登录页面之外的所有页面上。 I can't use look arounds for the regex so I am attempting to use the following but it isn't working. 我不能在正则表达式中使用环顾四周,因此我尝试使用以下内容,但它不起作用。

^/.*[^(credit|signin)].* Which should capture all urls except those containing credit or signin ^/.*[^(credit|signin)].*应该捕获所有URL,除了包含credit或signin的URL

[ indicates the start of a character class and the [^ negation is per-character. [指示字符类的开始,并且[^否定是每个字符。 Thus your regular expression is "anything followed by any character not in this class followed by anything," which is very likely to match anything. 因此,您的正则表达式是“任何后跟该类中没有的任何字符,后跟任何东西”,这很可能匹配任何东西。

Since you are using specific strings, I don't think a regular expression is appropriate here. 由于您使用的是特定字符串,因此我认为此处不适合使用正则表达式。 It would be a lot simpler to check that credit and signin don't exist in the string, such as with JavaScript: 检查creditsignin是否不存在于字符串中会容易得多,例如使用JavaScript:

-1 === string.indexOf("credit") && -1 === string.indexOf("signin")

Or you could check that a regular expression does not match 或者您可以检查正则表达式是否匹配

false === /credit|signin/.test(string)

Whitelisting words in regex is generally pretty easy, and usually follows a form of: 将正则表达式中的单词列入白名单通常非常容易,通常采用以下形式:

^.*(?:option1|option2).*$

The pattern breaks down to: 模式分解为:

  • ^ - start of string ^ -字符串的开头
  • .* - 0 or more non-newline characters* .* -0个或多个非换行符*
  • (?: - open non-capturing group (?: -打开非捕获组
    • option1|option2 - | option1|option2 | separated list of options to whitelist 分离的选项列表到白名单
  • ) - close non-capturing group ) -关闭非捕获组
  • .* - 0 or more non-newline characters .* -0个或多个非换行符
  • $ - end of string $ -字符串结尾

Blacklisting words in a regex is a bit more complicated to understand, but can be done with a pattern along the lines of: 将正则表达式中的单词列入黑名单要理解起来有些复杂,但是可以通过以下方式实现:

^(?:(?!option1|option2).)*$

The pattern breaks down to: 模式分解为:

  • ^ - start of string ^ -字符串的开头
  • (?: - open non-capturing group (?: -打开非捕获组
    • (?! - open negative lookahead (the next characters in the string must not match the value contained in the negative lookahead) (?! -打开否定前瞻(字符串中的下一个字符不得与否定前瞻中包含的值匹配)
      • option1|option2 - | option1|option2 | separated list of options to blacklist 分开的黑名单选项列表
    • ) - close negative lookahead ) -否定前瞻
    • . - a single non-newline character* -单个非换行符*
  • ) - close non-capturing group ) -关闭非捕获组
  • * - repeat the group 0 or more times * -重复该组0次或更多次
  • $ - end of string $ -字符串结尾

Basically this pattern checks that the values in the blacklist do not occur at any point in the string. 基本上,此模式检查黑名单中的值是否不会出现在字符串的任何位置。

* exact characters vary depending on the language, so use caution *确切的字符因语言而异,因此请谨慎使用


The final version: 最终版本:

/^(?:(?!credit|signin).)*$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM