I am trying to take a string of text and create an array from it so that the string:
var someText='I am some text and check this out! http://blah.tld/foo/bar Oh yeah! look at this too: http://foobar.baz';
insert magical regex here and
the array would look like this:
theArray[0]='I am some text and 开发者_如何学Gocheck this out! '
theArray[1]='http://blah.tld/foo/bar'
theArray[2]=' Oh yeah! look at this too: '
theArray[3]='http://foobar.baz'
I'm at a loss, any help would greatly be appreciated
--Eric
Split by URL regex (thanks to @Pullet for pointing out a flaw here):
var urlPattern = /(https?\:\/\/\S+[^\.\s+])/;
someText.split(urlPattern);
Let's break down the regex :)
(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+)
Running through your sample text gives,
["I am some text and check this out! ",
"http://blah.tld/foo/bar",
" Oh yeah! look at this too: ",
"http://foobar.baz",
""]
Try this:
<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", ""]
You could trim the individual results too, to not have leading and trailing whitespace on the non-url entries.
精彩评论