Understanding JavaScript String replace() method

Understanding JavaScript String replace() method

JavaScript string replace() method. In this tutorial, we will explain the JavaScript string.replace() method with the definition of this method, syntax, parameters, and several examples.

Javascript string replace() method. In this tutorial, we will explain the javascript string.replace() method with the definition of this method, syntax, parameters, and several examples.

JavaScript String Replace()

Definition: – The JavaScript string replace() method searches a string for a specified value or a regular expression, or keyword, and replace match search string to given string, returns a new string

Syntax

string.replace(search_value, new_value);

Parameters of string replace method

Javascript string replace()

Ex:-

var str = "My fav car color is black";
var res = str.replace(/blue/g, "silver");
document.write( "Output :- " + res ); 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript string replace() Method</title>
</head>
<body>
  <script type = "text/javascript">

    var str = "My fav car color is black";
    var res = str.replace(/blue/g, "silver");
    document.write( "Output :- " + res ); 

  </script>  
</body>
</html>

Result of the above code is:

Output :- My fav car color is silver

Ex-2

Here we will take the second example of string replace() method with case-insensitive string:

var str = "My fav bike color is Black";
var res = str.replace(/black/gi, "white");
document.write( "Output :- " + res ); 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript string replace() Method</title>
</head>
<body>
  <script type = "text/javascript">

    var str = "My fav bike color is Black";
    var res = str.replace(/black/gi, "white");
    document.write( "Output :- " + res ); 

  </script>  
</body>
</html>

Result of the above code is:

Output :- My fav bike color is white

Ex-3

Here we will take a new example of javascript string replace all with regexp.

var str = 'this is the sentence to end all sentences';

var res = str.replace(new RegExp('sentence', 'g'), 'message');

document.write( "Output :- " + res ); 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript replace all string</title>
</head>
<body>
  <script type = "text/javascript">

   var str = 'this is the sentence to end all sentences';

   var res = str.replace(new RegExp('sentence', 'g'), 'message');

   document.write( "Output :- " + res ); 

  </script>  
</body>
</html>

Result of the above code is:

Output :- this is the message to end all messages

JavaScript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Data Types In JavaScript

JavaScript data types are kept easy. While JavaScript data types are mostly similar to other programming languages; some of its data types can be unique. Here, we’ll outline the data types of JavaScript.

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.

Create a Line Through Effect with JavaScript

In this post we are going to create an amazing line through effect, with help of CSS and lots of JavaScript. So, head over to your terminal and create a folder LineThroughEffect. Create three files -index.html, main.js and styles.css inside it.

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.