正規表現に少し問題があります。
このURLでパスを取得しようとしていますvideoplay
。
http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello
この正規表現を使用すると、同様/.+
に一致/video
します。
含めないために、ある種のアンチ/ネガティブマッチが必要になります//
正規表現に少し問題があります。
このURLでパスを取得しようとしていますvideoplay
。
http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello
この正規表現を使用すると、同様/.+
に一致/video
します。
含めないために、ある種のアンチ/ネガティブマッチが必要になります//
JavaScript Webアプリでこれが必要な場合:このトピックで私がこれまでに見つけた最良の答えはここにあります。コードの基本(および元の)バージョンは次のようになります。
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"
ジョン・ロングありがとう、あなたは日ごとに作りました!
(http[s]?:\/\/)?([^\/\s]+\/)(.*)
グループ3
デモ: http: //regex101.com/r/vK4rV7/1
この式はvideoplay
、URLパスとも呼ばれる後にすべてを取得します。
/\/(videoplay.+)/
この式は、ポートの後にすべてを取得します。パスからも構成されます。
/\:\d./(.+)/
ただし、使用する場合Node.js
はネイティブurl
モジュールをお勧めします。
var url = require('url')
var youtubeUrl = "http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello"
url.parse(youtubeUrl)
これは、すべての正規表現が機能します。
{
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'
}
あなたはこれを試すことができます:
^(?:[^/]*(?:/(?:/[^/]*/?)?)?([^?]+)(?:\??.+)?)$
([^?] +)上記は、パスを返すキャプチャグループです。
これはすべてのURLの正規表現ではないことに注意してください。これは、「//」の後にある最初の「/」とそれに続く「?」の間のすべてのテキストを一致させるという問題を解決するだけです。キャラクター。
完全に一致する正規表現が必要な場合は、このStackOverflowリンクを確認して、URIのすべての可能性について説明し、「パス」を含む構成要素に分析することができます。
やり過ぎだと考え、入力URLが常に最初の「/」とそれに続く「?」の間にパスを持つパターンに従うことがわかっている場合は、上記の正規表現で十分です。
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') );
あなたはネガティブな後ろ姿を意味しますか?(?<!/)
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));
あなたはこれを試すことができます
/\"[a-z]+:\/\/[^ ]+/
使用法
if (/\"[a-z]+:\/\/[^ ]+/m.test(subject)) { // Successful match } else { // Match attempt failed }
新しいGoogle社員の場合は、任意の環境でJavaScript WebAPIURLを使用します。
new URL('your url string').pathname
これは正規表現ソリューションではありませんが、ほとんどの言語には、任意のURLを構成要素に解析するURLライブラリがあります。これはあなたがしていることのためのより良い解決策かもしれません。
これを試してください:
^http[s]?:\/\/(www\.)?(.*)?\/?(.)*
言語機能を使用した回答は適切ですが、REGEXPを使用してURLをコンポーネントに分割するもう1つの方法を次に示します。
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?
|| | | | | | | |
12 - scheme | | | | | | |
3 4 - authority, includes hostname/ip and port number.
5 - path| | | |
6 7 - query| |
8 9 - fragment
私はこれがあなたが求めているものだと思います:[^/]+$
デモ: http: //regex101.com/r/rG8gB9