Javascript 来自 URL 的正则表达式 URL 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12023430/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:55:15  来源:igfitidea点击:

Regex URL Path from URL

javascriptregexnode.jsurl

提问by ThomasReggi

I am having a little bit of regex trouble.

我有一点正则表达式问题。

I am trying to get the path in this url videoplay.

我正在尝试获取此 url 中的路径videoplay

http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello

If I use this regex /.+it matches /videoas well.

如果我使用这个正则表达式,/.+它也匹配/video

I would need some kind of anti / negative match to not include //

我需要某种反/否定匹配来不包括 //

采纳答案by ThomasReggi

This expression gets everything after videoplay, aka the url path.

此表达式获取 之后的所有内容videoplay,也就是 url 路径。

/\/(videoplay.+)/

This expression gets everything after the port. Also consisting of the path.

此表达式获取端口后的所有内容。也由路径组成。

/\:\d./(.+)/

However If using Node.jsI recommend the native urlmodule.

但是,如果使用Node.js我推荐本机url模块。

var url = require('url')
var youtubeUrl = "http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello"
url.parse(youtubeUrl)

Which does all of the regex work for you.

所有正则表达式都适合您。

{
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'video.google.co.uk:80',
  port: '80',
  hostname: 'video.google.co.uk',
  hash: '#hello',
  search: '?docid=-7246927612831078230&hl=en',
  query: 'docid=-7246927612831078230&hl=en',
  pathname: '/videoplay',
  path: '/videoplay?docid=-7246927612831078230&hl=en',
  href: 'http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello' 
}

回答by Vlad Mysla

In case if you need this for your JavaScript web-app: the best answer I ever found on this topic is here. Basic (and also original) version of the code looks like this:

如果你的 JavaScript 网络应用程序需要这个:我在这个主题上找到的最好的答案是这里。代码的基本(也是原始)版本如下所示:

var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";

parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.search;   // => "?search=test"
parser.hash;     // => "#hash"
parser.host;     // => "example.com:3000"

Thank you John Long, you made by day!

谢谢约翰龙,你白天做的!

回答by M G

(http[s]?:\/\/)?([^\/\s]+\/)(.*)group 3
Demo: http://regex101.com/r/vK4rV7/1

(http[s]?:\/\/)?([^\/\s]+\/)(.*)第 3 组
演示:http: //regex101.com/r/vK4rV7/1

回答by Vlad Mysla

function getPath(url, defaults){
    var reUrlPath = /(?:\w+:)?\/\/[^\/]+([^?#]+)/;
    var urlParts = url.match(reUrlPath) || [url, defaults];
    return urlParts.pop();
}
alert( getPath('http://stackoverflow.com/q/123/regex-url', 'unknown') );
alert( getPath('https://stackoverflow.com/q/123/regex-url', 'unknown') );
alert( getPath('//stackoverflow.com/q/123/regex-url', 'unknown') );
alert( getPath('http://stackoverflow.com/q/123/regex-url?foo', 'unknown') );
alert( getPath('http://stackoverflow.com/q/123/regex-url#foo', 'unknown') );
alert( getPath('http://stackoverflow.com/q/123/regex-url/', 'unknown') );
alert( getPath('http://stackoverflow.com/q/123/regex-url/?foo', 'unknown') );
alert( getPath('http://stackoverflow.com/q/123/regex-url/#foo', 'unknown') );
alert( getPath('http://stackoverflow.com/', 'unknown') );

回答by Kash

You can try this:

你可以试试这个:

^(?:[^/]*(?:/(?:/[^/]*/?)?)?([^?]+)(?:\??.+)?)$

([^?]+)above is the capturing group which returns your path.

([^?]+)上面是返回您的路径的捕获​​组。

Please note that this is not an all-URL regex. It just solves your problem of matching all the text between the first "/" occurring after "//" and the following "?" character.

请注意,这不是全 URL 正则表达式。它只是解决了匹配“//”之后出现的第一个“/”和随后的“?”之间的所有文本的问题。特点。

If you need an all-matching regex, you can check this StackOverflow linkwhere they have discussed and dissected all possibilities of an URI into its constituent parts including your "path".
If you consider that an overkill AND if you know that your input URL will always follow a pattern of having your path between the first "/" and following "?", then the above regex should be sufficient.

如果你需要一个全匹配的正则表达式,你可以查看这个StackOverflow 链接,他们在那里讨论了一个 URI 的所有可能性,并将其分解为它的组成部分,包括你的“路径”。
如果您认为这是一种矫枉过正,并且您知道您的输入 URL 将始终遵循在第一个“/”和“?”之间具有路径的模式,那么上面的正则表达式就足够了。

回答by Niet the Dark Absol

You mean a negative lookbehind? (?<!/)

你的意思是消极的回顾? (?<!/)

回答by Dexter's

var subject =
'<link rel="shortcut icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=ec617d715196"><link rel="apple-touch-icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a"><link rel="image_src" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">';
var re=/\"[a-z]+:\/\/[^ ]+"/m;
document.write(subject.match(re));

You can try this

你可以试试这个

/\"[a-z]+:\/\/[^ ]+/

Usage

用法

if (/\"[a-z]+:\/\/[^ ]+/m.test(subject)) {  // Successful match } else {    // Match attempt failed }

回答by Toby Allen

Its not a regex solution, but most languages have a URL library that will parse any URL into its constituent parts. This may be a better solution for what you are doing.

它不是正则表达式解决方案,但大多数语言都有一个 URL 库,可以将任何 URL 解析为其组成部分。对于您正在做的事情,这可能是更好的解决方案。

回答by Firas Dib

I think this is what you're after: [^/]+$

我认为这就是你所追求的: [^/]+$

Demo: http://regex101.com/r/rG8gB9

演示:http: //regex101.com/r/rG8gB9