javascriptregexuristring-matchingtrailing-slash

Matching a specific string between foward slash or # using regex


I'm trying to make this regex:

([?=section\/en\/]*)([^\/#]*)

For these examples:

https://www.test.com/en/string-to-get#cid=4949
https://www.test.com/en/section/string-to-get/page&2#cid=4949
https://www.test.com/en/section/string-to-get#cid=4949

current regex


Solution

  • You need to use

    (?<=section\/)([^\/#]*)
    

    Or, just

    section\/([^\/#]*)
    

    and grab Group 1 value.

    Here,

    See the regex demo #1 and regex demo #2.

    Depending on whether or not regex delimiters are required and if they are not /s you may use an unescaped /, (?<=section/)([^/#]*) and section/([^/#]*).