正则表达式提取精确(不包括)URL参数值

问题描述 投票:0回答:1

网址可能是

example.com/test?pid=123

example.com/test?test=ttt&pid=123

example.com/test#test=ttt&pid=123

example.com/test?pid=123&test=ttt&

example.com/test?xxxpid=123&test=ttt& //should not select because it has xxx prepanded.

它应该只选择pid参数值。我想出来了

[^?]+(?:\?pid=([^&]+).*)?

但这是有问题的。

example.com/test?gdsfg=sdfasdfa&pid=123
regex
1个回答
0
投票

选择pid参数值

.+[?&]pid=([^=?&\s]+)

所需的值在第一个捕获的组中

https://regex101.com/r/E1yHVY/11

© www.soinside.com 2019 - 2024. All rights reserved.