Created
April 22, 2016 15:58
-
-
Save tomasaschan/de83833cb324a35008ea765a6501a085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var regex = /\<(.*)(?:\|[^\|]+)?\>/g; | |
var testStrings = [ | |
"<http://this.is.a.url/foobar>", | |
"<https://this.too> and <http:/this.one> with some extra info", | |
"it doesn't have to start with a <net.tcp://odd.prodocol/>", | |
"and <no.protocol> is needed", | |
"This <http://one.has|A name>!", | |
"this isn't a match", | |
"this has a <http://google.com|link> inside other text", | |
"this has <http://a.pipe/?in=the|url|And a name> too" | |
]; | |
var regex = /\<((?:[^|>]+|\|(?=[^|>]*\|))*)(?:\|[^\|>]+)?\>/g; | |
var linksInMessage = function (text) { | |
var links = [], m; | |
while ((m = regex.exec(text)) !== null) { | |
if (m.index === regex.lastIndex) { | |
re.lastIndex++; | |
} | |
// View your result using the m-variable. | |
// eg m[0] etc. | |
links.push(m); | |
} | |
return links; | |
} | |
testStrings.forEach(function (str) { | |
console.log(str); | |
console.log(linksInMessage(str)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment