【问题标题】:reg should start with $, from then on match every full word seperated with a commareg 应该以 $ 开头,从那时起匹配每个用逗号分隔的完整单词
【发布时间】:2015-11-19 11:52:49
【问题描述】:

我正在尝试为此创建一个正则表达式:

.domain.com$object-subrequest,third-party,domain=domain.com|domain2.com|domain3.com|domain4.com

最好的结果是将它匹配成这样的结果:

- object-subrequest
- third-party
- domain
-- domain.com
-- domain2.com
-- domain3.com
-- domain4.com

但我不知道这是否可能。这样的结果也可以:

- object-subrequest
- third-party
- domain

然后另一个正则表达式过滤掉所有域,如下所示:

-- domain.com
-- domain2.com
-- domain3.com
-- domain4.com

到目前为止,我只能想出这个: https://regex101.com/r/wP8cY7/1

/(script|image|stylesheet|object|xmlhttprequest|subdocument|document|elemhide|other|third-party|domain|sitekey|match-case|collapse|donottrack),*/g

如您所见,这匹配包含其中一个单词的所有内容,我只需要 $ 之后的所有内容。 我只使用 Javascript(没有 jQuery)。

【问题讨论】:

  • 我看不出您列出的正则表达式与您要解决的问题有什么关系。它们似乎完全不相关。我在正则表达式中看到了 stylesheetelemhidedonottrack 之类的词。这些都没有出现在您的示例字符串中。这个正则表达式和你要解析的字符串有什么联系?
  • 我的例子是其中的一部分。所有这些词都是可能的匹配项。在我的示例中,我只使用了object-subrequestthird-partydomain。在 regex101 中,您会看到我目前使用的正则表达式存在问题,但该正则表达式并没有按照我希望的方式工作。

标签: javascript regex pattern-matching


【解决方案1】:

如果您可以通过其他方式摆脱$ 之前的所有内容,那么我认为这个正则表达式接近您想要的:

/[$,](script|image|stylesheet|object|xmlhttprequest|subdocument|document|elemhide|other|third-party|domain|sitekey|match-case|collapse|donottrack)/gi

【讨论】:

    【解决方案2】:

    在美元符号和逗号处拆分输入x;取除第一个分量的第一个分量之外的第一个分量的所有分量。然后用等号和或号分割:

    s <- strsplit(x, "[$,]")[[1]][-1]
    strsplit(s, "[=|]")
    

    给予:

    [[1]]
    [1] "object-subrequest"
    
    [[2]]
    [1] "third-party"
    
    [[3]]
    [1] "domain"      "domain.com"  "domain2.com" "domain3.com" "domain4.com"
    

    注意:我们使用这个作为输入x

    x <- ".domain.com$object-subrequest,third-party,domain=domain.com|domain2.com|domain3.com|domain4.com"
    

    【讨论】:

    • strsplit 看起来不像 javascript 函数。我希望使用正则表达式而不是拆分字符串。
    • 抱歉,不知道为什么这个问题出现在我的 R 问题列表中。
    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多