Introduction
Sometimes, we'll like to replace all occurrences of a string in JavaScript.
In this article, we'll present how to do that.
How to replace all occurrences of a string in JavaScript
To replace all occurrences of a string in JS we can use the replaceAll()
function introduced in ECMAScript 2021 specification.
If your browser does not support the replaceAll()
method you can use regular expression-based replacement:
Conclusion
To replace all occurrences of a string in JS we can use replaceAll()
method or string.replace()
with regexp function.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}