0

Consider the following code:

    var text2='ain';
    let text = "The rain in SPAIN stays mainly in the plain";
    let result = text.replaceAll(/text2/g,'__');
 
document.getElementById("demo").innerHTML = result;
<p id="demo"></p>

The string result is the same as text. How do I fix this problem? I just want every ain in text string to get replaced with ___.

CodeBug
  • 1,649
  • 1
  • 8
  • 23
M.Riyan
  • 79
  • 5
  • 1
    [How do you use a variable in a regular expression?](https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression) – Ivar Jul 26 '23 at 11:20
  • 2
    Just don't use a regex. `text.replaceAll(text2, '__');` should work. – Reyno Jul 26 '23 at 11:24
  • 1
    Does this answer your question? [How do I replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-do-i-replace-all-occurrences-of-a-string-in-javascript) – Damian Chudobiński Jul 26 '23 at 11:28
  • @DamianChudobiński Yes, it does. Thank you. But I wanted to use regular expression, so I learned it. – M.Riyan Jul 26 '23 at 11:35

3 Answers3

6

Try this

<script>
    const text2 = 'ain';
    const text = "The rain in SPAIN stays mainly in the plain";

    // Create a regular expression using the 'text2' variable as a string
    const regex = new RegExp(text2, 'g');

    // Use the 'regex' variable in the 'replaceAll' method
    const result = text.replaceAll(regex, '___');

    document.getElementById("demo").innerHTML = result;
</script>
Muhammad Bilal
  • 1,840
  • 1
  • 18
  • 16
0

Need to create a new regular expression using the RegExp constructor and pass the text2 variable and the 'g' flag to make the replacement global, you can use the following code:

<!DOCTYPE html>
<html>

<body>
    <p id="demo"></p>

    <script>
        const text2 = 'ain';
        const text = "The rain in SPAIN stays mainly in the plain";
        const regex = new RegExp(text2, 'g');
        const result = text.replaceAll(regex, '___');

        document.getElementById("demo").innerHTML = result;
    </script>

</body>

</html>

If you want to replace the 'AIN' in capital letters too. That can be done by adding the 'i' flag to the regular expression, then it becomes case-insensitive.

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
    const text2 = 'ain';
    const text = "The rain in SPAIN stays mainly in the plain";
    const regex = new RegExp(text2, 'gi');
    const result = text.replaceAll(regex, '___');
 
    document.getElementById("demo").innerHTML = result;
</script>

</body>
</html>
samith
  • 1,042
  • 8
  • 5
-1
<!DOCTYPE html>
<head>
<script type="text/javascript">
  let text2 = 'ain';
  let text = "The rain in SPAIN stays mainly in the plain";      
  let regex = new RegExp(text2, 'gi');
  let result = text.replace(regex, '---');    
  console.log(result); 
  document.getElementById("demo").innerHTML = result;
</script>
</head>
<body>
    <p id="demo"></p>
</body>
</html>
Sona Rijesh
  • 1,041
  • 7
  • 32
  • 62