博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

js 提取字符串url中的值

let baseUrlStr = 'https://!@!@!@@@@@!@/lock?code=DWE34234';
		const queryURLParameter = (url) => {
		  let regx = /([^&?=]+)=([^&?=]+)/g;
		  let obj = {};
		
		  url.replace(regx, (...args) => {
		    if (obj[args[1]]) {
		      obj[args[1]] = Array.isArray(obj[args[1]])
		        ? obj[args[1]]
		        : [obj[args[1]]];
		      obj[args[1]].push(args[2]);
		    } else {
		      obj[args[1]] = args[2];
		    }
		  });
 return obj;
		};
console.log(queryURLParameter(baseUrlStr).code);
posted @ 2022-05-19 13:47  八月情  阅读(163)  评论(0)    收藏  举报