How to replace string in Java Script?
Introduction:
In this article i will explain how to replace string in C#.
Description:
In previous articles i explained How to replace string in SQL Server and How to replace string in C#. Now i will explain how to replace string in Java Script. And how many ways we can replace the string in Java Script.
Example:
var oText = 'Hi sagar reddy';
var nText = oText.replace('sagar', 'vikram');
Output:
Hi vikram reddy
Now we will see more examples on this.
Example: (1)
var oText = 'asdAsdasd';
var nText = oText.replace('a', 'c');
Now it replaces the first occurance only.
Output:
csdasdasd
Example: (2)
Perform a global replacement:
var oText = 'asdAsdasd';
var nText = oText(/a/g, 'c');
Now it will replaces all the a's with c's.
Output:
csdAsdcsd
Example: (3)
Perform a global, case-insensitive replacement:
var oText = 'asdAsdasd';
var nText = oText.replace(/a/gi, 'c');
Output:
csdcsdcsd
-
CreatedAug 20, 2013
-
UpdatedOct 03, 2020
-
Views2,058
Related Articles
Check/Uncheck checkboxes when parent checkbox is checked/unchecked and vice-versa in Java Script
Set DropDownList value based on text/value in Java Script
Get selected text/value from DropDownList (or) Select in Java Script
Get selected index from DropDownList (or) Select in Java Script
Clear items from DropDownList (or) Select in Java script
How to validate number in Java Script
How to add or remove rows dynamically to HTML Table in Java Script
How to create HTML Table dynamically in Java Script
How to get or set value of a textbox in Java Script
How to get Key Codes in Java Script