javascript 正则表达式匹配 URI 中的与号,后跟等于号而不是另一个与号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17999427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to match ampersands in a URI that are followed by an equals and not another ampersand
提问by James
My regex knowledge is escaping me on this one...
我的正则表达式知识在这个问题上让我不知所措......
Say I have a URL with a URI as a query parameter, ala:
假设我有一个带有 URI 作为查询参数的 URL,ala:
http://hostname.com?uri=http://website.com/company/YoYo+&+Co+Inc&type=company
...assuming our uri param doesn't contain any params itself, I want to manually parse out the query params in Javascript, but obviously the ampersand in our embedded uri param makes it more difficult then simply splitting on all ampersands and running with it from there.
...假设我们的 uri 参数本身不包含任何参数,我想手动解析 Javascript 中的查询参数,但很明显,我们嵌入的 uri 参数中的&符号使它变得更加困难,然后简单地拆分所有&符号并使用它运行从那里。
What I really want to do is define a regex that matches only question marks and ampersands that are followed by an equals prior to being followed by another ampersand (or end of line). I came up with this which comes close but is including the non-capturing text as well and I'm not sure why:
我真正想要做的是定义一个正则表达式,它只匹配问号和与号,后跟一个等号,然后是另一个与号(或行尾)。我想出了这个接近但也包括非捕获文本,我不知道为什么:
[?&](?:[^&]+)=
...that results in a match on ?uri=
as well as &type=
which is close but capturing more than I want. What am I doing wrong such that it's not capturing just the ?
and &
in matches? In other words, it should only be capturing the ?
prior to uri and the &
prior to type.
...这导致匹配?uri=
以及&type=
接近但捕获的比我想要的更多。我做错了什么,以至于它不只捕获匹配中的?
和&
?换句话说,它应该只捕获?
优先于 uri 和&
优先于类型。
回答by JDiPierro
If I understand correctly and you just want to match the ? or & then your regex should be:
如果我理解正确并且您只想匹配 ? 或者 & 那么你的正则表达式应该是:
[?&](?==)
Explanation:
解释:
[?&]
is a set of characters containing just ? and &. Meaning it will look for one of those.
[?&]
是一组只包含 ? 和 &。这意味着它将寻找其中之一。
(?= )
This is a positive lookahead. It means "This has to come after the main match but don't include it". So to make it find an = looks kind of funny as (?==)
(?= )
这是一个积极的展望。这意味着“这必须在主要比赛之后进行,但不要包括它”。所以为了让它找到一个 = 看起来有点有趣(?==)
If you want to include the word "uri" or "type" then add a \w
after the character set and before the lookahead:
如果要包含单词“uri”或“type”,则\w
在字符集之后和前瞻之前添加一个:
[?&]\w+(?==)
+
means "match 1 or more"
+
意思是“匹配 1 个或多个”
And just one more in case that's not exactly what you're looking for! If you want to get rid of the &/? but keep the text we'd wrap the character set in a positive lookBEHIND. The syntax for that is (?<= )
. That would change the regex to this:
还有一个,以防万一这不是您要找的东西!如果你想摆脱 &/? 但保留文本,我们将字符集包装在一个积极的后面。其语法是(?<= )
. 这会将正则表达式更改为:
(?<=[?&])\w+(?==)
Example of that at work: http://regexr.com?35q0u
工作中的例子:http: //regexr.com?35q0u
In reponse to comment: You can match just the ? and & by putting the \w+ inside of the positive lookahead:
回应评论:你可以只匹配 ? 和 & 通过将 \w+ 放在积极的前瞻中:
[?&](?=\w+=)
And because I'm bored and like regexs a bit too much, here's one that will match the value of the tag:
而且因为我很无聊并且有点太喜欢正则表达式,所以这里有一个与标签的值相匹配的:
(?<==).*?(?=[&?]\w+=|$)
Example: http://regexr.com?35q11There's multiple highlighted sections because global matching is on.
示例:http: //regexr.com?35q11 由于启用了全局匹配,因此有多个突出显示的部分。