开发者

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\-\@?^=%&\/~\+#])?/;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜