简体   繁体   English

将URL中括号内的字符串转换为查询参数:Javascript

[英]Convert String in the Parenthesis in the URL To Query Parameters: Javascript

I want to convert the string in {} in search URL to query parameters which would help users capture search terms in web analytics tools. 我想将搜索URL中{}中的字符串转换为查询参数,这将帮助用户捕获Web分析工具中的搜索词。

Here's what I am trying to do, Let's say 这就是我想做的事

Search URL is: 搜索网址是:

example.com/search/newyork-gyms?dev=desktop&id=1220391131 example.com/search/newyork-gyms?dev=desktop&id=1220391131

User Input will be: 用户输入将是:

var search_url_format = '/search/{city}-{service}

Output URL: 输出网址:

example.com/search?city=newyork&service=gyms&dev=desktop&id=1220391131 example.com/search?city=newyork&service=gyms&dev=desktop&id=1220391131

The problem is when is use the regex {(.*)} it captures the whole string {city}-{service} . 问题是何时使用正则表达式{(.*)}会捕获整个字符串{city}-{service}

But I what I want is [{city},{service}] . 但是我想要的是[{city},{service}]

The URL format can also be like URL格式也可以像

search/{city}/{service}/ 搜索/ {城市} / {}服务/

search/{city}_{service}/ 搜索/ {城市} _ {}服务/

What I have tried is for a single variable. 我尝试过的是单个变量。

It returns correct output. 它返回正确的输出。

Eg: URL:/search/newyork 例如: URL:/search/newyork

User Input: /search/{city} 用户输入: /search/{city}

Output: /search/newyork?city=newyork 输出: /search/newyork?city=newyork

URL: /search-germany 网址: /search-germany

User Input: /search-{country} 用户输入: /search-{country}

Output: /search-germany?country=germany 输出: /search-germany?country=germany

 var search_url_format = '/search/{city}' //User Enters any variable in brackets var urloutput = '/search/newyork' //Demo URL //Log output console.log(URL2Query(search_url_format)) //Output: '/search/newyork?city=newyork' function URL2Query(url) { var variableReg = new RegExp(/{(.*)}/) var string_url = variableReg.exec(url) var variable1 = string_url[0] //Capture the variable var reg = new RegExp(url.replace(variable1, '([^\\?|\\/|&]+)')) var search_string = reg.exec(urloutput)[1] if (location.search.length > 0) // if no query parameters { return urloutput + "?" + string_url[1] + "=" + search_string } else { return urloutput + "&" + string_url[1] + "=" + search_string } } 

You can probably assume your "variable" will be alphanumeric instead of any character. 您可能假设您的“变量”将是字母数字而不是任何字符。 With this assumption "{", "-", "_" etc will be punctuation. 在此假设下,“ {”,“-”,“ _”等将是标点符号。

so your grouping regexp could be /({\\w+})/g . 因此您的分组正则表达式可能是/({\\w+})/g

//example 
const r = /({\w+})/g;
let variable;
const url = '/search/{city}-{service}';
while ((variable = r.exec(url)) !== null) {
 let msg = 'Found ' + variable[0] + '. ';
 msg += 'Next match starts at ' + r.lastIndex;
 console.log(msg);
}

You are missing two things: parenthesis to match groups and you use .* which includes "{" sign. 您缺少两件事:括号以匹配组,并且您使用了。*,其中包括“ {”符号。 So can use match instead of exec like this: 因此可以这样使用match而不是exec:

var search_url_format = '/search/{city}-{service}' //User Enters any    
var variableReg = new RegExp(/({\w+})/g)
var string_url = url.match(variableReg); // [{city}, {service}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM