Rex match a string in JS

I have few set of strings as mentioned below


I would like to get users in all four cases as output using regex in Javascript

I tried below in


It doesn’t seem to come up right.
Any help on this would be appreciated.

2 thoughts on “Rex match a string in JS”

  1. You were close with the positive lookbehind. This works:


    This matches users because after the lookbehind you match everything until just before the next slash.

  2. You may also do it without using regex using split function. This is assuming the index of ‘users’ won’t change.

    Function code:

    function getSubstring(str) {
      let subStr = str.split('/')[2];
      if(subStr.indexOf('?') === -1) {
        return subStr;
      } else {
    // Sample test

Leave a Comment