Bogdan Doncea
Bogdan Doncea

Reputation: 99

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.

Upvotes: 3

Views: 744

Answers (1)

Igor Bykov
Igor Bykov

Reputation: 2812

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.

Upvotes: 2

Related Questions