How to Use String Replace using Node Js?

Jan 13, 2022 . Admin



Hello Friends,

This article will give you example of replace all string occurrences in nodejs. I explained simply about node.js string replace all appearances. This tutorial will give you simple example of nodejs string replace() method.

In this post, you'll learn node.js string replace code. This is a short guide on how to replace a string in nodejs.

Here i will give you many example of how to replace all string occurrences in nodejs.

Example 1 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

let temp = 'This is Wesite.';
let newStr = temp.replace(/wesite/i, 'MyWebtuts');

console.log(newStr);

now you can simply run by following command:

node server.js
Output :
This is MyWebtuts.

Example 2 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

let temp = /(\w+)\s(\w+)/;
let tempStr = 'Node Js';
let tempResult = tempStr.replace(temp, '$2 $1');

console.log(tempResult);

now you can simply run by following command:

node server.js
Output :
Js Node

It will help you...

#Node JS