正则表达式/ Javascript帮助 - 搜索URL术语解析

时间:2022-08-26 13:16:11

I am building a 'keyword' highlighting script and I need to write a regular expression to parse the following url, to find the searched for keywords to highlight.

我正在构建一个'关键字'突出显示脚本,我需要编写一个正则表达式来解析以下网址,找到要搜索的关键字以突出显示。

I am REALLY bad with regex, so I was hoping someone who rocks it, could help.

我对正则表达式真是太糟糕了,所以我希望有人撼动它,可以提供帮助。

Our search string uses "skw" as the parameter and "%2c" (comma) to separate terms, with "+" for spaces.

我们的搜索字符串使用“skw”作为参数,使用“%2c”(逗号)来分隔术语,使用“+”作为空格。

Example URLS:

http://[url].com/Search.aspx?skw=term1 http://[url].com/Search.aspx?skw=term1%2c+term2

Is there a single RegExp that I can use that will give me a collection that looks like this?

是否有一个我可以使用的RegExp会给我一个看起来像这样的集合?

var "http://[url].com/Search.aspx?skw=term+one%2c+term2".match([Expression]);

matches[0] = "term one"
matches[1] = "term2"

Thanks! Any help is greatly appreciated.

谢谢!任何帮助是极大的赞赏。

3 个解决方案

#1


https?://[^\?]+\?skw=([^%]*)(?:%2c\+*(.*))?

In javascript this is

在javascript中这是

var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^%]*)(?:%2c\+*(.*))?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
    var term1 = match[1];
    var term2 = match[2];
}

EDIT:

Sorry, I re-read your question, to handle multiple terms you need to combine this with a split

对不起,我重新阅读了您的问题,处理了将其与拆分相结合所需的多个术语

var subject = "http://[url].com/Search.aspx?skw=term1+one%2c+term2";
var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^&]*)?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
  var terms = unescape(match[1]).split(",");
}

#2


You can't do this with a single match, but you can do it with a matches, a replace and a split:

你不能用一个匹配来做到这一点,但你可以用匹配,替换和拆分来做到这一点:

url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ").split('%2c')

You may want to do the match separately and bail out if the match fails (which it could if yur URL didn't have an skw parameter).

您可能希望单独进行匹配并在匹配失败时进行挽救(如果您的URL没有skw参数,则可能会失败)。

You probably really want to do an unescape too, to handle other escaped characters in the query:

你可能真的想要做一个unescape,来处理查询中的其他转义字符:

unescape(url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ")).split(',')

#3


This task doesn't really lend itself to a single regular expression. Check out Parsing Query Strings in JavaScript for a script to assist you.

这个任务并不适合单个正则表达式。在JavaScript中查看解析查询字符串以获取帮助您的脚本。

#1


https?://[^\?]+\?skw=([^%]*)(?:%2c\+*(.*))?

In javascript this is

在javascript中这是

var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^%]*)(?:%2c\+*(.*))?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
    var term1 = match[1];
    var term2 = match[2];
}

EDIT:

Sorry, I re-read your question, to handle multiple terms you need to combine this with a split

对不起,我重新阅读了您的问题,处理了将其与拆分相结合所需的多个术语

var subject = "http://[url].com/Search.aspx?skw=term1+one%2c+term2";
var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^&]*)?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
  var terms = unescape(match[1]).split(",");
}

#2


You can't do this with a single match, but you can do it with a matches, a replace and a split:

你不能用一个匹配来做到这一点,但你可以用匹配,替换和拆分来做到这一点:

url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ").split('%2c')

You may want to do the match separately and bail out if the match fails (which it could if yur URL didn't have an skw parameter).

您可能希望单独进行匹配并在匹配失败时进行挽救(如果您的URL没有skw参数,则可能会失败)。

You probably really want to do an unescape too, to handle other escaped characters in the query:

你可能真的想要做一个unescape,来处理查询中的其他转义字符:

unescape(url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ")).split(',')

#3


This task doesn't really lend itself to a single regular expression. Check out Parsing Query Strings in JavaScript for a script to assist you.

这个任务并不适合单个正则表达式。在JavaScript中查看解析查询字符串以获取帮助您的脚本。