Как запросить один параметр из строки параметров, разделенных & амперсандом?

Есть строка 'st.cmd=friendMain&st.friendId=123456789&st.layer.cmd=PopLayerClose&st._forceSetHistory=true' нужно из нее вытащить параметр friendId. Можно ли это сделать изящнее чем регэкспами?


Ответы (1 шт):

Автор решения: Pavel Nazarian

let friendId = new URL('http://localhost/?st.cmd=friendMain&st.friendId=123456789&st.layer.cmd=PopLayerClose&st._forceSetHistory=true').searchParams.get('st.friendId');
console.log(friendId);

→ Ссылка