Skip to content

Commit 6dafa8b

Browse files
authored
replaceAll
1 parent a9bd1cd commit 6dafa8b

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

README.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
|30 | [Private class methods and fields](#Private-class-methods-and-fields)|
3737
|31 | [Preventing paste into an input field](#Preventing-paste-into-an-input-field)|
3838
|32 | [The void operator](#The-void-operator)|
39+
|33 | [replaceAll](#replaceAll)|
3940

4041

4142

@@ -660,8 +661,8 @@ pasteBox.onpaste = (e) => {
660661
661662
662663
**[⬆ Back to Top](#table-of-contents)**
663-
### The void operator
664-
The void operator evaluates the given expression and then returns undefined.
664+
### replaceAll
665+
the method string.replaceAll(search, replaceWith) replaces all appearances of search string with replaceWith.
665666
```javascript
666667

667668

@@ -675,3 +676,43 @@ void anyfunction(); //returns undefined
675676
676677
```
677678
679+
680+
**[⬆ Back to Top](#table-of-contents)**
681+
### The void operator
682+
The void operator evaluates the given expression and then returns undefined.
683+
```javascript
684+
685+
686+
const str = 'this is a JSsnippets example';
687+
688+
const updatedStr = str.replace('example', 'snippet'); // 'this is a JSsnippets snippet'
689+
690+
691+
The tricky part is that replace method replaces only the very first match of the substring we have passed:
692+
693+
694+
const str = 'this is a JSsnippets example and examples are great';
695+
696+
const updatedStr = str.replace('example', 'snippet'); //'this is a JSsnippets snippet and examples are great'
697+
698+
In order to go through this, we need to use a global regexp instead:
699+
700+
701+
const str = 'this is a JSsnippets example and examples are great';
702+
703+
const updatedStr = str.replace(/example/g, 'snippet'); //'this is a JSsnippets snippet and snippets are greatr'
704+
705+
but now we have new friend in town, replaceAll
706+
707+
const str = 'this is a JSsnippets example and examples are great';
708+
709+
const updatedStr = str.replaceAll('example', 'snippet'); //'this is a JSsnippets snippet and snippets are greatr'
710+
711+
```
712+
713+
714+
715+
716+
717+
718+

0 commit comments

Comments
 (0)