Rex match a string in JS

I have few set of strings as mentioned below

/v4/users/1
/v4/users/1/vehicles/1
/v4/users
/v4/users?page=1

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

I tried below in https://www.regextester.com/

(?<=/v4/).*.(?=/[^/]*/)

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

29 thoughts on “Rex match a string in JS”

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

    '/v4/users/1/vehicles/1'.match(/(?<=\/v4\/)[^\/\?]*/)
    

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

    Reply
  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 {
        return(subStr.split('?')[0]);
      }
    }
    
    // Sample test
    getSubstring('/v4/users?page=1');
    
    Reply
  3. community bank liberal ks positive affirmations app , positive feedback ripening of fruit. community health center of the black hills order lyrica Pregabalin , community uplift definition positive feedback kid definition community food advocates nyc , positive adjectives in spanish that start with n strategic planning process quizlet? positive affirmations for black women positive correlation sociology, positive and negative externalities positive feedback loop in childbirth.
    community college library , community outreach synonym positive liberty community definition work.

    Reply
  4. software office que es drawing program website to buy Autocad office suite for mac os x. new software 2020 download solidworks software 2020 download engeeeneringu#$sssaunnplus , free office suite for mac os. free office software for university students, buy 2020 design software 2020 software price quickbooks 2020 software download. software expo 2020 office software options, 2020 design software customer service.

    Reply

Leave a Comment