-1

I have below code and i am trying to replace regex with another text but it is not working any suggestion what. i am doing wrong?

const regex = /\?/g;

let p = document.querySelectorAll('.test');

p.forEach((pa) => {
    pa.style.color = 'red';
    pa.innerHTML.replace(regex, 'test');
});
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>


<body>
    <p class="test">This one was, hopefully, quite straightforward although? it did get a bit more complicated as a? previous exercise had created multiple paragraph tags on the page. It’s a good bit of string! manipulation practice too.Got your own solution for this?;</p>
</body>
<script src="app2.js"></script>

</html>
kohatian
  • 67
  • 5

1 Answers1

1

Like Ivar mentioned you need to set the innerhtml.

pa.innerHTML = pa.innerHTML.replace(regex, 'test');

const regex = /\?/g;

let p = document.querySelectorAll('.test');

p.forEach((pa) => {
    pa.style.color = 'red';
    pa.innerHTML = pa.innerHTML.replace(regex, 'test');
});
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>


<body>
    <p class="test">This one was, hopefully, quite straightforward although? it did get a bit more complicated as a? previous exercise had created multiple paragraph tags on the page. It’s a good bit of string! manipulation practice too.Got your own solution for this?;</p>
</body>
<script src="app2.js"></script>

</html>
timmmmmb
  • 674
  • 7
  • 26