doujiao2000 2014-09-02 12:46
浏览 15
已采纳

regexpneed用减号和其他人找到单词

i have for now few types of code that i need to find and replace with regex.

  1. {word1/G_KP8zXsDp8/word2}
  2. {word1/GKP8zXsDp8/word2}
  3. {word1/G-KP8zXsDp8/word2}

my replacement now is: /({word1\/)(\w+)\/(\w+)/ and it finds 1st and 2nd cases, but don't find 3rd one. and I need it what it would be in match[2] $2.

  • 写回答

1条回答 默认 最新

  • doujia9204 2014-09-02 12:48
    关注

    The problem is that \w doesn't match a hyphen by default — it is a shorthand representation for the character class [a-zA-Z0-9_].

    To fix this, you can update your regex to include - as well. Use [\w-]+ instead of \w+.

    /({word1\/)([\w-]+)\/(\w+)/
    

    RegEx Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?