Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»How to Replace String in JavaScript

    How to Replace String in JavaScript

    By RahulJune 13, 20222 Mins Read

    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.

    Advertisement

    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

    Related Posts

    20 Common JavaScript Interview Questions and Answers

    JavaScript Arrays Tutorial for Beginner's

    JavaScript Arrays: A Beginner’s Guide

    How to Get the Current Timestamp in JavaScript

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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