-2

I need to filter out params from my URL. So for example:

URL = test1/test2/:test3

some filter output = test3

URL = test1/:test2/test3

some filter output = test2

URL = :test1/test2/test3 

some filter output = test1

URL = :test1/test2/test3/:test4 

some filter output = test1, test4

I've tried some REGEX and substring filters myself, but I can't really get it to work considering all possible URL examples above in 1 filter.

Fraction
  • 11,668
  • 5
  • 28
  • 48
juju
  • 316
  • 1
  • 3
  • 11
  • Why don't you show us your regex? –  Apr 25 '19 at 13:41
  • 1
    Possible duplicate of [Getting all URL parameters using regex](https://stackoverflow.com/questions/14679113/getting-all-url-parameters-using-regex) – Marten Apr 25 '19 at 13:41
  • Add the code of what you have tried to your question – reisdev Apr 25 '19 at 13:41
  • For example URL.substring(URL.indexOf(":")+1) works for example 1 with output 'test 3'. But it doesnt work for the remainders. Also the 'duplicate suggestions' are different. – juju Apr 25 '19 at 13:44

3 Answers3

1

You are actually just creating another router:

https://github.com/krasimir/navigo

I've never used navigo, but it looks powerful enough:

router
  .on('/user/:id/:action', function (params) {
    // If we have http://example.com/user/42/save as a url then
    // params.id = 42
    // params.action = save
  })
  .resolve();

router.notFound(function (query) {
  // ...
});

Now use it:

router.navigate('/products/list');

//Or even with FQDN:
router.navigate('http://example.com/products/list', true);
RajmondX
  • 405
  • 2
  • 9
  • My question is not related on how to navigate or use route. I need to filter out the parameters and save them in a list (for instance) – juju Apr 25 '19 at 13:47
1

Something like this?:

https://regex101.com/r/Jh0BVJ/3/

regex = new RegExp(/:(?<match>(?:[\w\-.~:?#\[\]@!$&'()*+,;=%]*)\d+)+/gm)
url = ":test1/test2/test3/:test4"
console.log(url.match(regex))
dave
  • 62,300
  • 5
  • 72
  • 93
0

const REGEX = /\:([A-z0-9-._~:?#\[\]\@\!\$\&\'\(\)\*\+\,\;\=]+)/g;

function findMatches(re, str, matches = []) {
   const m = re.exec(str)
   m && matches.push(m[1]) && findMatches(re, str, matches)
   return matches
}

const urls = [
  'test1/test2/:test3',
  'test1/:test2/test3',
  ':test1/test2/test3',
  ':test1/test2/test3/:test4',
];


const values = urls.map(url => findMatches(REGEX, url));

console.log(values);
teimurjan
  • 1,875
  • 9
  • 18