-1

I am new to regex expressions. I want every word in string between two character. have already tried followings Stack Questions:

  1. Get Substring between two characters using javascript

  2. Regular expression to get a string between two strings in Javascript

but nothing working for me i am missing something for sure. but confused what.

Suppose a "string": /path/__mutation/module1/__resolver/module2
Get directories between character: "__" and "/" // console.log() => [mutation, resolver] 

But trying above two questions anwser to construct something which can get all special directories like mutation, resolver in over path.

Following are what i have tried.

var __path = /path/__mutation/module1/__resolver/module2

1. console.log(__path.match(/__(.*)?\//g))
// [ '__mutation/login/withMobileNumber/' ]
// [ '__mutation/register/withMobileNumber/' ]
// [ '__mutation/raw/__mutation/register/__resolver/rock/' ]

2. console.log(__path.match(new RegExp(`__` + '(.*)' + `/`)))
// null
consider `Template_String` as variable with those symbols
...
Manu Yadav
  • 115
  • 1
  • 9

1 Answers1

1

First split on '/' and then filter on /^__.+/, then map to trim:

let path = '/path/__mutation/module1/__resolver/module2'
path.split(/\//).filter(p => /^__.+/.test(p)).map(p => p.replace(/__/,''))

// yields:  (2) ["mutation", "resolver"]
varontron
  • 1,120
  • 7
  • 21