考虑一下URL,如
https://stackoverflow.com/v1/summary/1243PQ/details/P1/9981
http://stackoverflow.com/v2/summary/saas?test=123我需要一个正则表达式来匹配这些URL并将它们转换为
stackoverflow.com:v1:summary:1243PQ:details:P1:9981
stackoverflow.com:v2:summary:saas我需要使用regex构建一个规则,在这里我可以使用$1、$2等提取路径,而无需使用任何javascript逻辑,因为我需要在分类规则生成器工具中使用它。我尝试过这个URL包含^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?,并提取了返回stackoverflow.com:v1/summary/1243PQ/details/P1/9981的$4:$5
但是,这是不正确的。有人能帮我找出正确的标准吗?
发布于 2021-05-27 07:51:04
你可以试试这个:
正则表达式
/(?:https?:\/\/([^\/?\s#]+))?\/([^\/?\s#]*)(?:[\?#].*)?/g替换
$1:$2(?: non-capturing group
https?:\/\/ "http://" or "https://"
([^\/?\s#]+) capture the domain and put it in group 1
)? make this capture optional
\/ "/"
([^\/?\s#]*) one segment of the url path, capture it in group 2
(?:[\?#].*)? an optional non-capturing group for consuming query string or # anchor at the end检查测试用例
更新
如果不能使用g标志进行替换,那么没有其他更好的方法,只能野蛮地执行所有组合:
您需要为url路径的每个段添加一个\/([^\/?#\s]+)和:$2等:
https://stackoverflow.com^https?:\/\/(?:www\.)?([^\/?#\s]+)\/?(?:[#?].*)?$$1https://stackoverflow.com/path1^https?:\/\/(?:www\.)?([^\/?#\s]+)\/([^\/?#\s]+)\/?(?:[#?].*)?$$1:$2https://stackoverflow.com/path1/path2^https?:\/\/(?:www\.)?([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/?(?:[#?].*)?$$1:$2:$3https://stackoverflow.com/path1/path2/path3^https?:\/\/(?:www\.)?([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/?(?:[#?].*)?$$1:$2:$3:$4https://stackoverflow.com/path1/path2/path3/path4^https?:\/\/(?:www\.)?([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/?(?:[#?].*)?$$1:$2:$3:$4:$5https://stackoverflow.com/path1/path2/path3/path4/path5^https?:\/\/(?:www\.)?([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/([^\/?#\s]+)\/?(?:[#?].*)?$$1:$2:$3:$4:$5:$6https://stackoverflow.com/questions/67716688
复制相似问题