From c275ba48b66d165d885decb220f130d93d832acd Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sun, 13 Dec 2020 21:17:05 +0300 Subject: [PATCH] closes #2382 --- .../17-regexp-methods/article.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/9-regular-expressions/17-regexp-methods/article.md b/9-regular-expressions/17-regexp-methods/article.md index 9654b142..0d5168b4 100644 --- a/9-regular-expressions/17-regexp-methods/article.md +++ b/9-regular-expressions/17-regexp-methods/article.md @@ -228,6 +228,23 @@ alert(result); // Smith, John Using a function gives us the ultimate replacement power, because it gets all the information about the match, has access to outer variables and can do everything. +## str.replaceAll(str|regexp, str|func) + +This method is essentially the same as `str.replace`, with two major differences: + +1. If the first argument is a string, it replaces *all occurences* of the string, while `replace` replaces only the *first occurence*. +2. If the first argument is a regular expression without the `g` flag, there'll be an error. With `g` flag, it works the same as `replace`. + +The main use case for `replaceAll` is replacing all occurences of a string. + +Like this: + +```js run +// replace all dashes by a colon +alert('12-34-56'.replaceAll("-", ":")) // 12:34:56 +``` + + ## regexp.exec(str) The method `regexp.exec(str)` method returns a match for `regexp` in the string `str`. Unlike previous methods, it's called on a regexp, not on a string.