javaScript Replace() | javaScript String Replace All

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_val, new_val);

Parameters of string replace method

| Parameter | Description |
| search_val | This is the first parameter and required. The value, or regular expression, that will be replaced by the given value |
| new_val | This is the second parameter and required. The given value to replace the search value. |

Ex:

   var str = "My fav bike color is white";
   var res = str.replace(/white/g, "black");
   document.write( "Output :- " + res ); 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>javaScript Replace() |javaScript String Replace All</title>
</head>
<body>
     
</body>
<script type = "text/javascript">
   var str = "My fav bike color is white";
   var res = str.replace(/white/g, "black");
   document.write( "Output :- " + res ); 
</script>  

</html>

Result of the above code is:

Output :- My fav bike color is black

Ex

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

   var str = "My fav bike color is White";
   var res = str.replace(/white/gi, "black");
   document.write( "Output :- " + res ); 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>javaScript Replace() |javaScript String Replace All</title>
</head>
<body>
     
</body>
<script type = "text/javascript">
   var str = "My fav bike color is White";
   var res = str.replace(/white/gi, "black");
   document.write( "Output :- " + res ); 
</script>  

</html>

Result of the above code is:

Output :- My fav bike color is black

Ex3

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() |javaScript String Replace All</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 #programming

javaScript Replace() | javaScript String Replace All
1 Likes39.75 GEEK