- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript String replace
JavaScript Strings
JavaScript String replace
replaceswaps one piece of text for another and returns a new string.
By default it only changes the first match.
replaceAll changes every one, which is usually what people expect.
Example
Example
javascript
const text = "cat cat cat";
console.log(text.replace("cat", "dog"));Only the first cat changed.
replace and replaceAll
replace changes the first match only.
replaceAll changes every match.
Both return a new string and leave the original alone.
