链接的正则表达式

时间:2016-09-21 14:53:43

标签: javascript regex

我需要修剪这个字符串 - > http://hokuspokus/ixdebude/ix我完全需要这部分字符串 / ixdebude / ix

我有2个表达式来解决我的问题。

我的第一个正则表达式 - >

/(.*)\/(.*)(\/)(.*)\/*/g返回Array [5]组。

我的第二个正则表达式 - >

/(\/............)$(.*?)$/g返回 / ixdebude / ix

你们有人可以给我另一个正则表达式,没有千点的更好吗?

2 个答案:

答案 0 :(得分:4)

您可以使用这样的正则表达式:

.text-info

<强> Working demo

对于javascript,您可以使用:

//.*?(/.*)

答案 1 :(得分:3)

你可以试试这个

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"

https://gist.github.com/jlong/2428561