R
R
Roman Govorov2020-05-14 14:42:53
JavaScript
Roman Govorov, 2020-05-14 14:42:53

How to use JS to find a value in a string and replace it and some more?

There is a line site.ru/?reg=77&data=55
We need to find reg and replace everything between = and &

But the script must be universal, so that even if I need to replace the last value, for example date, then everything after equal must be replaced there /

How to implement it? Can't catch up.. and replace used and indexOf

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-05-14
@RGameShow

const searchParams = new URLSearchParams(location.search);

searchParams.set('reg', 'new_value'); // Меняем данные
searchParams.delete('data'); // Удаляем данные

location.search = searchParams;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question