0

次のような文字列があります: "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。一般的な正規表現はどうあるべきですか?

4

2 に答える 2

1

あなたはおそらくしたいです

/^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"]
于 2012-08-25T05:23:00.493 に答える
0

探している正規表現は次のとおりです:/^http\://www.google.com/hello/world/(.*/)d.htm$/

http://jsfiddle.net/rDvJK/

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");
于 2012-08-25T05:16:04.453 に答える