代码之家  ›  专栏  ›  技术社区  ›  S16

使用正则表达式在字符串第n次出现后捕获

  •  1
  • S16  · 技术社区  · 2 年前

    我的测试字符串:

    /custom-heads/food-drinks/51374-easter-bunny-cake
    

    我试图捕捉字符串中的数字。该字符串中的常量是数字,数字前面总是有3 / 然后是一个 - .

    我是一个regex noob,正在努力解决这个问题。我拼凑起来 (\/)(.*?)(-) 然后我想我可以通过编程得到最后一个,但我真的很想更好地理解正则表达式,如果有人能给我看正则表达式,让我得到最后一个出现在 / - .

    3 回复  |  直到 2 年前
        1
  •  1
  •   Maielo    2 年前

    如果可能的话,不要使用正则表达式,我建议你阅读- https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ 博客帖子

    对于你的问题,使用劈裂更容易、更快、更防弹

    const articleName = "/custom-heads/food-drinks/51374-easter-bunny-cake".split("/")[3]
    // '51374-easter-bunny-cake'
    
    const articleId = articleName.split("-")[0]
    
    // '51374'
    

    希望有帮助

        2
  •  0
  •   anubhava    2 年前

    您可以将此正则表达式与捕获组一起使用:

    ^(?:[^\/]*\/){3}([^-]+)
    

    或者在现代浏览器中,可以使用lookback断言:

    /(?<=^(?:[^\/]*\/){3})[^-]+/
    

    RegEx Demo 1

    RegEx Demo 2

    正则表达式代码:

    • ^ :开始
    • (?:[^\/]*\/){3} :匹配0个或多个非- / 字符后跟 / .重复这组动作3次
    • ([^-]+) :匹配1+个非连字符

    代码:

    const s = `/custom-heads/food-drinks/51374-easter-bunny-cake`;
    
    const re = /^(?:[^\/]*\/){3}([^-]+)/;
    
    console.log (s.match(re)[1]);
        3
  •  0
  •   Ryszard Czech    2 年前

    使用

    const str = `/custom-heads/food-drinks/51374-easter-bunny-cake`
    const p = /(?:\/[^\/]*){2}\/(\d+)-/
    console.log(str.match(p)?.[1])

    看见 regex proof .

    解释

    Non-capturing group (?:\/[^\/]*){2}
    {2} matches the previous token exactly 2 times
    \/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
      Match a single character not present in the list below [^\/]
      * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      \/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
    \/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
    1st Capturing Group (\d+)
      \d matches a digit (equivalent to [0-9])
      + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
    - matches the character - with index 4510 (2D16 or 558) literally (case sensitive)