0

I want to replace all [27] with 80 but not getting the expected output.

html:

<p id="demo">if('[28]' == 'Individual'){var b = [27] }else{ b = [27]/[29] } if('Yes' == 'Yes'){var a = b * [30] }else{ a = 0 } a</p>
<button onclick="myFunction()">Try it</button>

js:

function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var res = str.replace(/[27]/g, "80");
    document.getElementById("demo").innerHTML = res;
}

final output which i am getting using this function is.

  if('[808]' == 'Individual'){var b = [8080] }else{ b = [8080]/[809] } if('Yes' == 'Yes'){var a = b * [30] }else{ a = 0 } a

It is replacing every matched digit with 80.I have also tried

var res = str.replace("[27]", "80");

but it only replacing the first occurrence of matched string.

Also my exact string in my project is

console.log(`[${this.inputIdsWatching[index]}]`); // it gives [27]
console.log(this.inputsWatching[index]);   // it gives 80
calculationCriteria.replace(`[${this.inputIdsWatching[index]}]`, `${this.inputsWatching[index]}`);

So how do apply regex in it ?

swapnil jain
  • 252
  • 1
  • 3
  • 22

0 Answers0