RegEx - Get All Characters After Last Slash in URL

后端 未结 8 2187
无人共我
无人共我 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: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:

    (?!.*\/).+

提交回复
热议问题