YouTip LogoYouTip

Jsref Replaceall

# JavaScript replaceAll() Method [![Image 4: String Object Reference Manual](#) JavaScript String Object](#) ## Example In this example, we replace all occurrences of "Microsoft" with "": var str="Visit Microsoft! Visit Microsoft!"; var n=str.replaceAll("Microsoft",""); _n_ Output: Visit !Visit ! [Try it Β»](#) * * * ## Definition and Usage The replaceAll() method is used to replace some characters with other characters in a string, or to replace a substring that matches a regular expression. This function replaces all matching substrings. If you want to learn more about regular expression tutorials, please see our (#) and our (#). This method does not change the original string. * * * ## Browser Support !(#) The IE browser on PC does not support this function. * * * ## Syntax const newStr = str.replaceAll(regexp|substr, newSubstr|function) ## Parameter Values | Parameter | Description | | :--- | :--- | | _regexp|substr_ | Required. Specifies the substring or the RegExp object of the pattern to be replaced. Note that if this value is a string, it is treated as a literal text pattern to search for, rather than being first converted to a RegExp object. When using a regex, you must set the global ("g") flag, otherwise, it will throw a TypeError: "String.prototype.replaceAll called with a non-global RegExp argument". | | _newSubstr|function_ | Required. A string value. Specifies the replacement text or a function that generates the replacement text. | ## Return Value | Type | Description | | --- | --- | | String | A new string, obtained after replacing all matches of regexp with newSubstr. | ## Technical Details | JavaScript Version: | 1.2 | | --- | * * * ## More Examples ## Example Perform a global replacement: var str="Mr Blue has a blue house and a blue car"; var n=str.replaceAll(/blue/ig,"red"); _n_ Output: Mr red has a red house and a red car. [Try it Β»](#) * * JavaScript String Object](#)
← Js HoistingFunc Array Key Last β†’