title: Javascript .replace not working with .match words when more than two same words are to be replaced
body: <p>I cannot understand why my code is not working when I am about to replace three exact same words. Lets say if an input is "www.facebook.com www.facebook.com wwww.facebook.com"</p>
  
  <p>my output would be this which is not what I want </p>
  
  <pre><code>&lt;div class="content" style="font-size:13px;"&gt;
  &lt;a href="www.facebook.com"&gt;www.facebook.com&lt;/a&gt;
  &lt;a href="&amp;lt;a href=" www.facebook.com'=""&gt; www.facebook.com&lt;/a&gt;'&amp;gt; www.facebook.com www.facebook.com
  &lt;/div&gt;
  </code></pre>
  
  <p>but if I input lets say "www.facebook.com www.google.com wwww.yahoo.com" I get what I want</p>
  
  <pre><code>&lt;div class="content" style="font-size:13px;"&gt;
  &lt;a href="www.facebook.com"&gt;www.facebook.com&lt;/a&gt;
  &lt;a href=" www.google.com"&gt; www.google.com&lt;/a&gt; 
  &lt;a href="wwww.yahoo.com"&gt;wwww.yahoo.com&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>What I am actually trying to do is when a user inputs a link in a text area and submits it. The code will replace the match link with an anchor tag.</p>
  
  <p>This is my .js</p>
  
  <pre><code>var wordlink2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
  var hyperlink2;
  
  hyperlink2 = content.match(wordlink2);
  
  var $item = $("#ClassroomCommentComment").val();
  
  
  if (hyperlink2 != null) {
      for (x = 0; x &lt; hyperlink2.length; x++) {
          var site2 = hyperlink2[x].toString();
          var z2 = $item.replace(hyperlink2[x], "&lt;a href='" + site2 + "'&gt;" + site2 + "&lt;/a&gt;");
          $item = z2;
      }
  }​
  </code></pre>
  
  <p>I am already blown away with this. Can anyone help me with this :(</p>
  
  <p><strong>just an additional this is my function to be more precise:</strong> </p>
  
  <pre><code>function checkSubmit()
      {
          var $item = $("#ClassroomCommentComment").val();
          var $cont = $("#FileFile").val();
  
  
          if($item == "")
          {
              return false;
          }
          else
          {
              //$("#ClassroomCommentComment").css('color', 'white');
  
              if (hyperlink != null)
              {
                  for (x= 0; x&lt;hyperlink.length; x++)
                  {
  
                      var site = hyperlink[x].toString();
                      var z = $item.replace(hyperlink[x],"&lt;a href='" + site + "'&gt;" + site +"&lt;/a&gt;" );
                      $item = z;
                  }       
              }
  
              if (hyperlink2 != null)
              {               
                  for (x= 0; x&lt;hyperlink2.length; x++)
                  {
                      var site2 = hyperlink2[x].toString();
                      var z2 = $item.replace(hyperlink2[x],"&lt;a href='" + site2 + "'&gt;" + site2 +"&lt;/a&gt;" );
                      $item = z2;
  
                  }       
              }
  
  
  
              for (x=0; x&lt;=arraycontroller; x++)
              {
                  var n = $item.replace(fullnamewp[x],"&lt;a href = 'http://www.classoncloud.org/" + userlink[x] + "'&gt;" + fullnamewp[x] +"&lt;/a&gt;" );
                  $item = n;
                  //$("#ClassroomCommentComment").val(n);
              }
  
              if (hash != null)   
              {
                  for (x= 0; x&lt;hash.length; x++)
                  {
                      var new_item = hash[x].toString().substr(1).split("#");
                      var b = $item.replace(hash[x],"&lt;a href='http://www.classoncloud.org/results/" + new_item + "'&gt;" + hash[x] +"&lt;/a&gt;" );
                      $item = b;
                  }   
              }
  
              arraycontroller = 0;
              fullnamewp = [];
              userlink = [];
  
  
              var classroom_id = $("#ClassroomCommentClassroomId").val();
              var user_id = $("#ClassroomCommentUserId").val();
              var comment = $item;
              var image = "&lt;?php echo $profile_pic; ?&gt;";
              var name = "&lt;?php echo $current_user_name; ?&gt;";
  
  
  
  
          $.ajax({
          type: "POST",   
          url: '/learns/classroom/',
          data: "data[ClassroomComment][classroom_id]=" + classroom_id + "&amp;data[ClassroomComment][user_id]=" + user_id + "&amp;data[ClassroomComment][comment]=" + comment,
          dataType: "html",
          success: function(data){
  
  
  
          var parent_id = data;
  
  
  
          $('#comment-list').prepend('&lt;div class="comments"&gt;  &lt;a href="'+user_id+'"  class="image"&gt;&lt;img src="'+image+'" width="50" height="50"&gt;   &lt;/a&gt;&lt;a href="#" class="wall_delete" id='+parent_id+' style="display: none;"&gt;&lt;/a&gt;    &lt;div class="name"&gt;  &lt;a href="'+user_id+'" class="close"&gt;X&lt;/a&gt;&lt;a href="#" style="font-size:13px;"&gt;'+name+'&lt;/a&gt;&lt;div class="content" style="font-size:13px;"&gt;'+comment+'&lt;/div&gt; &lt;div class="time"&gt;&lt;div class="star-count"&gt;&lt;/div&gt;&lt;a class="my-star" href="#" id='+parent_id+'&gt;myStar .&lt;/a&gt;&lt;a class="click" href="#"&gt;Comment&lt;/a&gt; 1 seconds&amp;nbsp;ago &lt;/div&gt;&lt;/div&gt;       &lt;form id="LearnsClassroomForm" accept-charset="utf-8" method="post" action="/learns/classComment"&gt;&lt;input type="hidden" name="data[ClassroomComment][parent_id]" action="classroom" value='+parent_id+' id="ClassroomCommentParentId"&gt;&lt;input type="hidden" name="data[ClassroomComment][classroom_id]" action="classroom" value="'+classroom_id+'"id="ClassroomCommentClassroomId"&gt;&lt;input type="hidden" name="data[ClassroomComment][user_id]" action="classroom" value="'+user_id+'" id="ClassroomCommentUserId"&gt;&lt;div class="displaycomment"&gt;&lt;textarea name="data[ClassroomComment][comment]" class="textarea" placeholder="Write comment here..." id="ClassroomCommentComment" style="overflow: hidden; word-wrap: break-word; resize: none; height: 18px;"&gt;&lt;/textarea&gt; &lt;input type="submit" value="Comment" class="submit" style=" margin-right:7px; margin-top:10px;"&gt; &lt;/div&gt; &lt;/form&gt;&lt;/div&gt;');
  
              }
          }); 
  
  
          $("#ClassroomCommentComment").val("");
          $("#ClassroomCommentComment").css('color', 'black');
  
          $($(this).parent().parent().find("textarea")).css("height","30px");     
          return false;   
  
          }   
  
      }
  </code></pre>