Navigate back to the homepage

how to replace string in node js?

Infinitbility
Node
July 6th, 2022 · 1 min read
how to replace string in node js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To replace string in node js, use the replace() and use replace() with /PUT_HERE_REPLACEABLE_STRING/g regex method.

  1. the replace() method replace your first occurrence ( match ) from your string
  2. the replace() method with regex will replace all occurrences from your string

Just you have to pass two parameters, in the first parameter you have to pass what you want to replace and in the second parameter you have to pass what you want to put in the place of replace string.

In the following example, we will take the samples string, and use both replace methods for replacing space with a hyphen ( - ).

1let str = "hi friends welcome to infinitbility".
2
3str.replace(" ", "-"); // "hi-friends welcome to infinitbility"
4
5str.replaceAll(/ /g, "-"); // "hi-friends-welcome-to-infinitbility"

Today, I’m going to show you How do i replace strings in node js, as mentioned above here, I’m going to use the replace() or replace() method with regex.

Let’s start today’s tutorial how do you replace strings in node js?

In this node js example, we will do

  1. take an example of a string variable
  2. use the replace() or replace() method with regex
  3. example of replacing space with a hyphen
  4. example of replacing string with another string
  5. example of removing space from a string
  6. print the output in the node js console
1var http = require("http");
2
3//create a server object:
4http
5 .createServer(function (req, res) {
6 let str = "hi boys hi girls welcome to infinitbility";
7
8 // Example of replace space with hypen
9 console.log("Example of replace space with hypen");
10 let replaceSWH = str.replace(" ", "-");
11 let replaceAllSWH = str.replace(/ /g, "-");
12
13 console.log(replaceSWH);
14 console.log(replaceAllSWH);
15
16 // Example of replace string with another string
17 console.log("Example of replace string with another string");
18 let replaceSWS = str.replace("hi", "hello");
19 let replaceAllSWS = str.replace(/hi/g, "hello");
20
21 console.log(replaceSWS);
22 console.log(replaceAllSWS);
23
24 // Example of remove space from string
25 console.log("Example of remove space from string");
26 let replaceSWN = str.replace(" ", "");
27 let replaceAllSWN = str.replace(/ /g, "");
28
29 console.log(replaceSWN);
30 console.log(replaceAllSWN);
31
32 res.write(
33 str +
34 "\n\n" +
35 replaceSWH +
36 "\n" +
37 replaceAllSWH +
38 "\n\n" +
39 replaceSWS +
40 "\n" +
41 replaceAllSWS +
42 "\n\n" +
43 replaceSWN +
44 "\n" +
45 replaceAllSWN
46 );
47 res.end(); //end the response
48 })
49 .listen(8080); //the server object listens on port 8080

In the above node js example, we have taken the sample string variable and performed an example of replacing space with a hyphen, replacing string with another string, and removing space from a string.

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to check if string contains substring in react js?

how to check if string contains substring in react js?

An example of check string contains substring in react js with a step-by-step example.

July 5th, 2022 · 1 min read
how to generate random number in react js?

how to generate random number in react js?

An example of generate random number in react js with a step-by-step example.

July 5th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]