String.replace

replace — Replaces a string with a string based on a string or regular expression.

Description

String.replace(mixed comparison, string with)

Parameters

Name Description Type Default Optional
comparison String or regexp mixed No
with String to replace with string No

Return values

string primitive.

Examples

Example #1 – replace example
console.log('Hello World'.replace('Hello', 'Foo')); // Foo World console.log('Hello World'.replace('o', 'a')); // Hella World console.log('Hello World'.replace(/o/g, 'a')); // Hella Warld

External references