如何根据 URL 中符号后的第一个字符串设置变量

How to set a variable based on the first string in the URL after a symbol?

本文关键字:第一个 字符串 设置 变量 符号 何根 URL      更新时间:2023-09-26

目前我正在这样做:

var token = document.location.href.split('?_sft_category=')[1];

如果我的 URL 是:

http://www.example.com/xchanges/results/?_sft_category=sky

在这种情况下,我sky作为变量,但是如果我的 URL 变成

http://www.example.com/xchanges/results/?_sft_category=sky#comboFilters%5BAgency%5D=.TBWA

在这种情况下,我显然在=之后得到所有内容,而相反,我只想在'?_sft_category='之后得到第一个字符串

url = "http://www.example.com/xchanges/results/?_sft_category=sky";
alert(url.split('?_sft_category=')[1].match(/[A-z]+/));
url ="http://www.example.com/xchanges/results/?_sft_category=sky#comboFilters%5BAgency%5D=.TBWA";
alert(url.split('?_sft_category=')[1].match(/[A-z]+/));

演示