RegEx - Get All Characters After Last Slash in URL

后端 未结 8 2143
无人共我
无人共我 2020-11-29 07:00

I\'m working with a Google API that returns IDs in the below format, which I\'ve saved as a string. How can I write a Regular Expression in javascript to trim the string to

相关标签:
8条回答
  • 2020-11-29 07:49

    This should work:

    last = id.match(/\/([^/]*)$/)[1];
    //=> nabb80191e23b7d9
    
    0 讨论(0)
  • 2020-11-29 07:52

    this is easy to understand (?!.*/).+

    let me explain:

    first, lets match everything that has a slash at the end, ok? that's the part we don't want

    .*/ matches everything until the last slash

    then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"

    (?!.*) this is "Negative lookahead"

    Now we can happily take whatever is next to what we don't want with this .+

    YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:

    (?!.*\/).+

    0 讨论(0)
提交回复
热议问题