ReplaceAll in javascript and dollar sign

I have a string with three dollar signs, for example. $$$Test123. I would like to display this line in a div. The problem is that when I use replace, I get $$Test123- $ 2 instead of 3.

Example:

var sHtml="<_content_>";
var s="$$$Test";
sHtml= sHtml.replace("<_content_>", s);

Now the result sHtmlis $$Test;

Any idea how this can be solved?

+3
source share
4 answers

javascript does not have a default function that replaces all functions. You can write your own like this



function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);

}

+8
source

$has special meaning when included in a string for the second argument of a call replace(). Usually you should use it to refer to matched expressions in the source string. For instance:

"foo foooo".replace(/fo+/g, "$&bar");
//-> "foobar foooobar"

$& , foo foooo .

$. $ , , . 3 $, 6 :

var sHtml="<_content_>";
var s="$$$$$$Test";  
sHtml= sHtml.replace("<_content_>", s);
//-> "$$$Test"
+3

-

var sHtml="<_content_>"  
var s="$$$Test";  
sHtml= sHtml.replace("<_content_>", s);
+1

replaceAll:

http://www.dumpsite.com/replaceAll.php

javascript , , .

. replaceAll.

This function will produce the expected result.

Try and submit your fee.

0
source

All Articles