36
36
| 30 | [ Private class methods and fields] ( #Private-class-methods-and-fields ) |
37
37
| 31 | [ Preventing paste into an input field] ( #Preventing-paste-into-an-input-field ) |
38
38
| 32 | [ The void operator] ( #The-void-operator ) |
39
+ | 33 | [ replaceAll] ( #replaceAll ) |
39
40
40
41
41
42
@@ -660,8 +661,8 @@ pasteBox.onpaste = (e) => {
660
661
661
662
662
663
**[⬆ 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 .
665
666
` ` ` javascript
666
667
667
668
@@ -675,3 +676,43 @@ void anyfunction(); //returns undefined
675
676
676
677
```
677
678
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