0

私はJavaScriptの正規表現が初めてです..

これは私の問題です:

http://example/uploads/files/full/images/image.png

私はちょうど取得したいですuploads/files/full/images/image.png

regex を使用してどのように行うことができますか?

誰でも助けてください..ありがとう..

4

2 に答える 2

3
var someString = "http://example/uploads/files/full/images/image.png";
var path = someString.match(/https?:\/\/example\/(.+)$/)[1];

http://jsfiddle.net/Squeegy/QwHsC/

// matches http or https
https?

// matches :// since you have to escape slashes
:\/\/

// matches the domain name
example/

// captures the path all the way to the end of the string
(.+)$
于 2012-12-24T06:37:21.280 に答える
3

将来、この種のことのためにより包括的なライブラリを使用したい場合 (つまり、必要に応じて特定の正規表現を構築/要求するのではなく)、次のようなものを見ることができます:

http://blog.stevenlevithan.com/archives/parseuri

その場合、

parseUri("http://example/uploads/files/full/images/image.png").path

戻るだろう

uploads/files/full/images/image.png
于 2012-12-24T06:43:18.083 に答える