Regular expressions are by far the best way to do this. But, since this is for educational purposes, here is a method for finding URLs in strings. This is not the right answer, but shows concepts.
Let's say you have this text below:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis faucibus dui nec est auctor auctor. Nullam vulputate, augue non auctor rhoncus, urna urna tempus velit, non tincidunt nibh orci et elit. Integer sed nibh massa, eget vestibulum velit. www.Nulla.com at elit vitae massa egestas accumsan ac semper nisl. Curabitur viverra lorem at urna porttitor sit amet aliquam sapien http://imperdiet.org . Pellentesque Scholars of the Sagittis of Malleswad. Mauris lobortis aliquam felis sit amet vestibule. In the biography of faucibus lectus. Donec consectetur ante nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse Potenti.
There are two links here. The code is as follows.
Somehow get the text in a variable, which we will call text .
var text = '<that text above>';
Now we will split the text into an array called words .
var words = text.split(' ');
In this example, we will create new output, so we need a string variable to store it.
var new_text = '';
You seem to understand loops, but this is exactly what most loops passing through an array look.
for (var i=0; i < words.length; i++) { var word = words[i];
We check whether the beginning of this "word" (which refers to each section of the text, separated by spaces, in this context) contains any of our prefixes. In fact, bit.ly is a valid URL. Regular expressions have better control over this than we do.
if (word.indexOf('http://') === 0 || word.indexOf('www.') === 0) {
We change the meaning of the word by putting the syntax of the links. I used Markdown, but you can do HTML or whatever.
word = '[A LINK]' + '(' + word + ')'; }
This is our conclusion, which we talked about at the top of the script. Just keep adding things to it, no matter how they were changed or not. The problem with this may be 1, or 94 spaces after our word, but we always return one. Regular expressions have functionality to handle this better.
new_text += word + ' '; }
new_text now looks like this.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis faucibus dui nec est auctor auctor. Nullam vulputate, augue non auctor rhoncus, urna urna tempus velit, non tincidunt nibh orci et elit. Integer sed nibh massa, eget vestibulum velit. [A LINK](www.Nulla.com) in elit vitae massa egestas accumsan ac semper nisl. Curabitur viverra lorem in urna porttitor sit amet aliquam sapien [A LINK](http://imperdiet.org.) Pellentesque ultricies sagittis malesuada. Mauris lobortis aliquam felis sit amet vestibulum. In vitae faucibus lectus. Donec consectetur ante nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse potenti.
which on stack overflow looks like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis faucibus dui nec est auctor auctor. Nullam vulputate, augue non auctor rhoncus, urna urna tempus velit, non tincidunt nibh orci et elit. Integer sed nibh massa, eget vestibulum velit. LINK in the elite vitae massa egestas accumsan ac semper nisl. Curabitur viverra lorem in urna porttitor sit amet aliquam sapien A LINK Pellentesque ultricies sagittis malesuada. Mauris lobortis aliquam felis sit amet vestibulum. In vita faucibus lectus. Donec consectetur ante nisi. Lorem ipsum dolor sits amet, consectetur adipiscing elit. Suspendisse potenti.
Pay attention to the problem? The split method was seen at http://imperdiet.org . 'as a url. What the user had in mind was that " http://imperdiet.org " be the URL and have a period after it. Regular expressions also avoid this.
The demo has all the code without my comment between them.
If you want to know about regular expressions, I recommend this site .