Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»How to Replace String in JavaScript

    How to Replace String in JavaScript

    RahulBy RahulJune 13, 20222 Mins ReadUpdated:June 13, 2022

    We can use replace() method to replace any string or character with another in javascript. It searches for a defined string, character, or regular expression and replaces it. This method doesn’t change the original string but it returns the updated string as result.

    Syntax:

    string.replace(old_string, new_string)
    

    Replace String in JavaScript with Examples

    In this tutorial, we will discuss a few examples of replacing strings in Javascript.

    Let’s try some examples:

    • Here is the first example to initialize a text in a variable, then replace it with another text.

      1
      2
      3
      4
      let str = "Hello World!"
      let result = str.replace("World", "TecAdmin")
       
      console.log(result)

      Run the above example and see the results. Thanks to playcode.io that help me to run javascript online.

      How to Replace String in JavaScript
      Replace string in javascript
    • What happens if a given string is found multiple times. In that case, the replace() function will replace the first occurrence of the given string.

      1
      2
      3
      4
      5
      let str = "Mr Bean has a green Apple and a Red Apple"
       
      let result = str.replace("Apple", "Strawberries")
       
      console.log(result)

      Execute the above code and see the results:

      Replace first matching string in javascript
      Replace first matching string in javascript

      The result clearly shows that the first occurrence is replaced with a new string, but the second occurrence is not replaced. So, how can I replace all the occurrences of a given string? Let’s check the next example:

    • We can also define the regular expression and the function will replace all occurrences matching that regular expression.

      See the below example, where we defined a regular expression to replace string globally.

      1
      2
      3
      4
      5
      6
      let str = "Mr Bean has a green Apple and a red Apple"
       
      const regex = "/Apple/ig"
      let result = str.replace("Apple", "Strawberries")
       
      console.log(result)

      Run the above example and see the results.

      Replace string with regular expression in JavaScript
      Replace string with regular expression in JavaScript
    • Basically, the regular expression is used to match patterns. To replace all occurrences of any string, we can use replaceAll() function.

      The below example uses the replaceAll() function in javascript.

      1
      2
      3
      4
      5
      let str = "Mr Bean has a green Apple and a red Apple"
       
      let result = str.replaceAll("Apple", "Strawberries")
       
      console.log(result)

      Run the above code and see the results.

      Replace all matching string in JavaScript
      Replace all matching strings in JavaScript

    Wrap Up

    In this tutorial, we have discussed a few examples to replace a string in javascript.

    javascript js string
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install Anaconda on Ubuntu 22.04
    Next Article How to Create SFTP User in Ubuntu 22.04 (No Shell Access)

    Related Posts

    JavaScript Program to Add Two Numbers

    Updated:June 9, 20222 Mins Read

    Shell Script – Remove Double Quote (“”) from a String

    1 Min Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    How To Convert String to Integer in Python

    Updated:December 15, 20212 Mins Read

    10 Useful JavaScript Console Methods

    Updated:June 9, 20223 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    • How to Install Composer on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.