将字符串分解为text / http链接

时间:2010-07-15 03:26:06

标签: javascript regex

我正在尝试获取一串文本并从中创建一个数组,以便字符串:

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'

我很茫然,任何帮助都会非常感激

- 埃里克

2 个答案:

答案 0 :(得分:2)

按网址正则表达式拆分(感谢@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",
""]

答案 1 :(得分: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>

results =

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

您也可以修剪单个结果,以便在非url条目上没有前导和尾随空格。