A
A
Alexander Shiryaev2021-11-02 14:05:52
JavaScript
Alexander Shiryaev, 2021-11-02 14:05:52

How to choose a regular expression for url?

I have a url for which the regular expression should work.
Url like - mysite.com/param/sometext-alt-text/otherparam/text?...
Regular match should work on /param/ and /otherparam/
Help me choose a regular match. Thank you)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-11-02
@shiryaev585

So?

const links = [
  'mysite.com/param/sometext-alt-text/otherparam/text?zzzz',
  'mysite.com/param/foo/otherparam/qqqqqq',
  'mysite.com/param/123/otherparam/jfghjfghk?...',
  'mysite.com/param/foo/otherparam/gdfsghdfhkj',
  'mysite.com/param1/sometext-alt-text/otherparam/text?zzzz',
  'mysite.com/param2/foo/otherparam/qqqqqq',
  'mysite.com/param/123/otherparam3/jfghjfghk?...',
  'mysite.com/param/foo/otherparam4/gdfsghdfhkj',
];

const re = /param\/(?<param>.+)\/otherparam\/(?<otherparam>.*)/;
links.forEach(link => {
  const found = link.match(re);
  console.log(link, found);
});

and regular seasons are better not to select, but to compose.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question