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? Link to comment https://forums.phpfreaks.com/topic/177312-replace-each-url-with-string/ 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'); Link to comment https://forums.phpfreaks.com/topic/177312-replace-each-url-with-string/#findComment-934893 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 Link to comment https://forums.phpfreaks.com/topic/177312-replace-each-url-with-string/#findComment-934934 Share on other sites More sharing options...
Daniel0 Posted October 11, 2009 Share Posted October 11, 2009 Because you're matching (searching), not replacing... Link to comment https://forums.phpfreaks.com/topic/177312-replace-each-url-with-string/#findComment-934952 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 Link to comment https://forums.phpfreaks.com/topic/177312-replace-each-url-with-string/#findComment-934956 Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 you need the g modifier Link to comment https://forums.phpfreaks.com/topic/177312-replace-each-url-with-string/#findComment-934962 Share on other sites More sharing options...
phorcon3 Posted October 11, 2009 Author Share Posted October 11, 2009 thanks, man;) Link to comment https://forums.phpfreaks.com/topic/177312-replace-each-url-with-string/#findComment-934966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.