1

I would like to replace the string "Microsoft" with "W3Schools Test$".

Please note that there is single quotes after the dollar sign.

It is not working with my below code. You can see demo-one is working perfectly, but in case of demo-two its not working well.

function myFunction() {
    var str_one = document.getElementById("demo-one").innerHTML; 
    var res_one = str_one.replace("Microsoft", "W3Schools Test$");
    document.getElementById("demo-one").innerHTML = res_one;
    
    var str_two = document.getElementById("demo-two").innerHTML; 
    var res_two = str_two.replace("Microsoft", "W3Schools Test$'");
    document.getElementById("demo-two").innerHTML = res_two;
}
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<p id="demo-one">Visit Microsoft!</p>
<p id="demo-two">Visit Microsoft!</p>

<button onclick="myFunction()">Try it</button>

Here is the output of the above code.

Click the button to replace "Microsoft" with "W3Schools" in the paragraph below: Visit W3Schools Test$! Visit W3Schools Test!!

Third line should show as Visit W3Schools Test$' but it showing as Visit W3Schools Test!!

Please help me to get out from this problem.

Thanks in advance.

Neodan
  • 5,154
  • 2
  • 27
  • 38
Kushal
  • 2,605
  • 4
  • 22
  • 23
  • 1
    `I would like to replace the string "Microsoft" with "W3Schools Test$'"` out of the frying pan, into the fire – Jaromanda X Oct 26 '17 at 03:50
  • Possible duplicate of [JavaScript replace() method dollar signs](https://stackoverflow.com/questions/38866071/javascript-replace-method-dollar-signs) – Vivek Doshi Oct 26 '17 at 04:06

2 Answers2

4

In the context of replace , $' is use to inserts the portion of the string that follows the matched substring.

To insert $ using the string#replace, use $$.

function myFunction() {
    var str_one = document.getElementById("demo-one").innerHTML; 
    var res_one = str_one.replace("Microsoft", "W3Schools Test$");
    document.getElementById("demo-one").innerHTML = res_one;
    
    var str_two = document.getElementById("demo-two").innerHTML; 
    var res_two = str_two.replace("Microsoft", "W3Schools Test$$'");
    console.log(res_two);
    document.getElementById("demo-two").innerHTML = res_two;
}
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<p id="demo-one">Visit Microsoft!</p>
<p id="demo-two">Visit Microsoft!</p>

<button onclick="myFunction()">Try it</button>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

function myFunction() {
        var str_one = document.getElementById("demo-one").innerHTML; 
        var res_one = str_one.replace("Microsoft", "W3Schools Test$$'");
        document.getElementById("demo-one").innerHTML = res_one;
        
        var str_two = document.getElementById("demo-two").innerHTML; 
        var res_two = str_two.replace("Microsoft", "W3Schools Test$$'");
        document.getElementById("demo-two").innerHTML = res_two;
    }
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<p id="demo-one">Visit Microsoft!</p>
<p id="demo-two">Visit Microsoft!</p>

<button onclick="myFunction()">Try it</button>
Krishna Soni
  • 95
  • 1
  • 9