2010-07-15 66 views
2

我试图采取文本字符串,并从它创建一个数组,这样的字符串:分手字符串转换成文本/ HTTP链接

var someText='I am some text and check this out! http://blah.tld/foo/bar Oh yeah! look at this too: http://foobar.baz'; 

此处插入神奇的正则表达式和

数组应该是这样的:

theArray[0]='I am some text and check this out! ' 
theArray[1]='http://blah.tld/foo/bar' 
theArray[2]=' Oh yeah! look at this too: ' 
theArray[3]='http://foobar.baz' 

我不知所措,任何帮助将大大理解

--Er IC

+0

您的意思是说每次找到URL时应该拆分字符串? – kiamlaluno 2010-07-15 03:35:32

+0

我知道这个字符串,我需要将它分解并重新放回去,一遍又一遍地翻过来, – Eric 2010-07-15 03:48:14

回答

2

通过URL正则表达式斯普利特(感谢@Pullet这里指出了一个缺陷):

var urlPattern = /(https?\:\/\/\S+[^\.\s+])/; 
someText.split(urlPattern); 

让我们打破正则表达式:)

 
(https? -> has "http", and an optional "s" 
\:\/\/  -> followed by :// 
\S+  -> followed by "contiguous" non-whitespace characters (\S+) 
[^\.\s+]) -> *except* the first ".", or a series of whitespace characters (\s+) 

运行通过你的示例文本给出,

["I am some text and check this out! ", 
"http://blah.tld/foo/bar", 
" Oh yeah! look at this too: ", 
"http://foobar.baz", 
""] 
+0

Anurag,非常感谢你 - 那就是诀窍!虽然我还在努力阅读! – Eric 2010-07-15 04:11:23

+0

@Eric - regexes很有趣:),更新后加解释 – Anurag 2010-07-15 04:30:04

+0

/(https*\:\/\/\S+[^\.\s+])/也会匹配httpssss://test.com,这不是一个有效的网址。 我想要的是/(https?\:\/\/\S+[^\.\s+])/ 这个?意味着前面的字符是可选的 虽然如果你想支持其他协议,下面的东西也可以工作(取决于你需要支持的协议数量) /((https?| s?ftp | gopher) \:\/\/\ S + [^ \。\ s +])/ – 2010-07-15 05:45:00

0

试试这个:

<script type="text/javascript"> 
    var url_regex = /((?:ftp|http|https):\/\/(?:\w+:{0,1}\w*@)?(?:\S+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:.?+=&%@!\-\/]))?)+/g; 
    var input = "I am some text and check this out! http://blah.tld/foo/bar Oh yeah! look at this too: http://foobar.baz"; 

    var results = input.split(url_regex); 
    console.log(results); 
</script> 

结果=

["I am some text and check this out! ", 
"http://blah.tld/foo/bar", 
" Oh yeah! look at this too: ", 
"http://foobar.baz", ""] 

你可以修剪单独的结果也是如此,不会有领导和非URL条目尾随空白。