AS3 Regex test for URL
I want to check if a string contains a URL.
var string:String = "Arbitrary amount of random text before and after.<br/><br/>http://www.somdomain.co.uk<br/><br/>Tel: 0123 4开发者_Go百科56 789<br/>";
var pattern:RegExp = new RegExp("(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?");
ExternalInterface.call('console.log',pattern.test(string));
This outputs false to my console, whereas when I feed the regex and string into http://gskinner.com/RegExr/, the url is found.
What am I doing wrong and how do I fix it?
I just escaped a couple extra /
and it started working. Also, I generally like to use the regex literal notation to create regexes as it gives you better syntax highlighting than converting a string to a regex in the new Regex()
:
var pattern:RegExp = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/;
精彩评论