次のような文字列があります: "http://www.example.com/hello/world/ab/c/d.html"
(またはこれ"http://www.example.com/hello/world/ab/d.html"
:)
http://www.example.com/hello/world/
との間のコンテンツを抽出したいd.html
。一般的な正規表現はどうあるべきですか?
次のような文字列があります: "http://www.example.com/hello/world/ab/c/d.html"
(またはこれ"http://www.example.com/hello/world/ab/d.html"
:)
http://www.example.com/hello/world/
との間のコンテンツを抽出したいd.html
。一般的な正規表現はどうあるべきですか?
あなたはおそらくしたいです
/^http:\/\/[^\/]*\/[^\/]*\/[^\/]*\/(.*)\/[^\/]*$/
この (複雑に見える) 式は、ドメインと最初の 2 つのパス コンポーネントをスキップし、最終パス コンポーネントの前のすべてのビットを抽出します。
例:
>>> 'http://www.google.com/hello/world/ab/c/d.html'.match(/^http:\/\/[^\/]*\/[^\/]*\/[^\/]*\/(.*)\/[^\/]*$/)
["http://www.google.com/hello/world/ab/c/d.html", "ab/c"]
探している正規表現は次のとおりです:/^http\://www.google.com/hello/world/(.*/)d.htm$/
function getIt(fromWhat) {
var matches = fromWhat.match(/^http\:\/\/www\.google\.com\/hello\/world\/(.*\/)d.htm$/);
console.log(matches);
return matches[1];
}
getIt("http://www.google.com/hello/world/ab/c/d.htm");