JavaScript String replace() MethodThe JavaScript string replace() method is used to replace a part of a given string with a new substring. This method searches for specified regular expression in a given string and then replace it if the match occurs. We can use global search modifier with replace() method to replace all the match elements otherwise the method replace only first match. JavaScript also provides ignore flag to make the method case-insensitive. SyntaxThe replace() method is represented by the following syntax: Parameteroriginalstr - It represents the string to be searched and replaced. Newstr - It represents the new string that replaced with the searched string. ReturnIt returns the new string with the specified replacement. JavaScript String replace() Method ExampleLet's see some examples of replace() method. Example 1Let's see a simple example to replace a substring. Test it NowOutput: JavaScript Example 2In this example, we will replace a regular expression using global search modifier. Test it NowOutput: Learn AngularJS on Javatpoint. AngularJS is a well-known JavaScript framework. Example 3In this example, we will replace a regular expression without using global search. Test it NowOutput: Learn AngularJS on Javatpoint. Node.js is a well-known JavaScript framework Example 4In this example, we will see that replace() method is case-sensitive. Test it NowOutput: Learn Node.js on Javatpoint. Node.js is a well-known JavaScript framework. Example 5We can ignore case-sensitive behaviour of replace() method by using ignore flag modifier. Let's understand with the help of example: Test it NowOutput: Learn AngularJS on Javatpoint. AngularJS is a well-known JavaScript framework. Next TopicJavaScript String |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India