Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Change Text from HTML file

I'm making a node.js project but I can't get something done:

How to change dynamically a text from a html file?

Here in the HTML file I have the following lines:

<div class="col-md-12 col-xl-12 offset-xl-5">
                        <code id=someId'><strong>{%CODE%}</strong></code>
                    </div>

and in the controller file in Node.js, I'm calling the replace function:

var index = fs.readFileSync(`${__dirname}/../view/index.html`, 'utf-8');

index = index.replace(/{%CODE%}/gm, 'someText');
fs.writeFileSync(`${__dirname}/../view/index.html`, index);

Now, the file will contain 'someText' instead of {%CODE%}. But, somewhen in the future, I will want to change it too.

Let's say it's like a calculator. I want to show the answer everytime, dynamically, not to hard code it.

In fact, I have an email checker site. I want to display if the email is or not valid, in the same page.

like image 537
Bogdan Doncea Avatar asked Dec 09 '25 00:12

Bogdan Doncea


1 Answers

Seems likely that multiline flag is missing.

data.replace(/{%CODE%}/gm, 'someText');

Also, read/writeFileSync don't accept callbacks, so the callback in readFileSync is simply ignored.

like image 147
Igor Bykov Avatar answered Dec 11 '25 14:12

Igor Bykov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!