J
J
JsNoobUser2021-11-23 14:11:52
JavaScript
JsNoobUser, 2021-11-23 14:11:52

How to make a regular expression to get the part of a string after a certain word?

How to make a regular expression to get the part of a string after a certain word?

https://mydomen.ru/category/search?area=32&city=31...

You need to separate the line after https://mydomen.ru/category/search

area city and so on can be different

Answer the question

In order to leave comments, you need to log in

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

Regular something like this: /\?(.*)/
https://regex101.com/r/5rpoUX/1/
Then you can split the string into pairs by sign &and then split each pair into key and value by sign =.
But it's much better to use the built-in URL interface in JS :

const url = new URL('https://mydomen.ru/category/search?area=32&city=31');
console.log('host', url.host);
console.log('search', url.search);
const city = url.searchParams.get('city');
console.log('city', city);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question