0

Below is my snippet where I have lots of ":)" but only one ":)" is replace, any ideas, help, clues, suggestions, recommendations please?

$(document).ready(function(){
  
  var mm = "Hello :) hai there :) :)";
  
  $("#mm").text(mm.replace(":)","[emo_smile]"));
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="mm"></div>
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • How about using while loop? `while(mm.indexOf(":)") != -1) mm = mm.relpace(":)", "[emo_smile]");` – Yonggoo Noh May 12 '16 at 06:38
  • 1
    Use RegExp: `.replace(/:\)/g,"[emo_smile]")` – Mephiztopheles May 12 '16 at 06:39
  • @Mephiztopheles I got this error "Uncaught SyntaxError: Invalid regular expression: /:)/: Unmatched ')'" after trying your suggestion. Any ideas? – Juliver Galleto May 12 '16 at 06:40
  • 1
    It's because you're using the wrong regular expression. Try the one Mephiztopheles suggested. You must escape the closing parentheses as it is a special character in regular expressions. – MicSokoli May 12 '16 at 06:52
  • yap :) the ")" character is a key used in regex to define (close) a capturing group so you have to escape it using backslash – Mephiztopheles May 12 '16 at 07:00

0 Answers0