phorcon3 Posted October 11, 2009 Share Posted October 11, 2009 var text = 'this is the first url http://www.example.com and this is the second url http://www.example2.com'; how do i match each of these urls and replace them with the word url? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 11, 2009 Share Posted October 11, 2009 Use Google to find a regular expression for URLs. Then do like: text.replace(/the regex here/, 'url'); Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted October 11, 2009 Author Share Posted October 11, 2009 how come this script below <script type="text/javascript"> var text = 'url 1 http://www.example.com url 2 http://www.example2.com haha'; var matches = text.match(/http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/); for(var i = 0; i < matches.length; i++) { document.write(matches[i]); } </script> only outputs: http://www.example.com Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 11, 2009 Share Posted October 11, 2009 Because you're matching (searching), not replacing... Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted October 11, 2009 Author Share Posted October 11, 2009 yah, i know, but i wanna extract both urls first and then replace each i just dont get why it only outputs http://www.example.com and not http://www.example.com http://www.example2.com Quote Link to comment Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 you need the g modifier Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted October 11, 2009 Author Share Posted October 11, 2009 thanks, man;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.