JavaScript | javascript string replace
Belowistheexampleofthestring.replace()Method.Example: Output:GfGThestring.replace()isaninbuiltmethodinJavaScriptwhichisusedtoreplaceapartofthegivenstringwithsomeanotherstringoraregularexpression.Theoriginalstringwillremainunchanged.Syntax:str.replace(A,B)Parameters:HeretheparameterAisregularexpressionandBisastringwhichwillreplacethecontentofthegive...
Below is the example of the string.replace() Method.
Example:<script>
var string = GeeksForGeeks;
var newstring = string.replace(/GeeksForGeeks/, GfG);
document.write(newstring);
</script>
Output: GfGThe string.replace() is an inbuilt method in JavaScript which is used to replace a part of the given string with some another string or a regular expression. The original string will remain unchanged.
Syntax:
str.replace(A, B)Parameters: Here the parameter A is regular expression and B is a string which will replace the content of the given string.
Return Values: It returns a new string with replaced items.
JavaScript code to show the working of this method:Code #1: Here the contents of the string GeeksForGeeks will be replaced with gfg.<script>
var string = GeeksForGeeks is a CS portal;
v...