Skip to content

Commit a8b1205

Browse files
authored
Merge pull request #293 from subhoghoshX/q-155
Question 155: add other string methods that accept RegExp
2 parents f7ddb9a + c3d843f commit a8b1205

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -3029,21 +3029,38 @@
30293029

30303030
**[⬆ Back to Top](#table-of-contents)**
30313031

3032-
155. ### What are the string methods available in Regular expression
3032+
155. ### What are the string methods that accept Regular expression
3033+
3034+
There are six string methods: search(), replace(), replaceAll(), match(), matchAll(), and split().
30333035

3034-
Regular Expressions has two string methods: search() and replace().
30353036
The search() method uses an expression to search for a match, and returns the position of the match.
30363037

30373038
```javascript
30383039
var msg = "Hello John";
30393040
var n = msg.search(/John/i); // 6
30403041
```
30413042

3042-
The replace() method is used to return a modified string where the pattern is replaced.
3043+
The replace() and replaceAll() methods are used to return a modified string where the pattern is replaced.
3044+
3045+
```javascript
3046+
var msg = "ball bat";
3047+
var n1 = msg.replace(/b/i, "c"); // call bat
3048+
var n2 = msg.replaceAll(/b/i, "c"); // call cat
3049+
```
3050+
3051+
The match() and matchAll() methods are used to return the matches when matching a string against a regular expression.
3052+
3053+
```javascript
3054+
var msg = "Hello John";
3055+
var n1 = msg.match(/[A-Z]/g); // ["H", "J"]
3056+
var n2 = msg.matchAll(/[A-Z]/g); // this returns an iterator
3057+
```
3058+
3059+
The split() method is used to split a string into an array of substrings, and returns the new array.
30433060

30443061
```javascript
30453062
var msg = "Hello John";
3046-
var n = msg.replace(/John/i, "Buttler"); // Hello Buttler
3063+
var n = msg.split(/\s/); // ["Hello", "John"]
30473064
```
30483065

30493066
**[⬆ Back to Top](#table-of-contents)**

0 commit comments

Comments
 (0)