Skip to content

Commit

Permalink
Merge pull request #293 from subhoghoshX/q-155
Browse files Browse the repository at this point in the history
Question 155: add other string methods that accept RegExp
  • Loading branch information
sudheerj authored Jul 10, 2024
2 parents f7ddb9a + c3d843f commit a8b1205
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3029,21 +3029,38 @@

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

155. ### What are the string methods available in Regular expression
155. ### What are the string methods that accept Regular expression

There are six string methods: search(), replace(), replaceAll(), match(), matchAll(), and split().

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

```javascript
var msg = "Hello John";
var n = msg.search(/John/i); // 6
```

The replace() method is used to return a modified string where the pattern is replaced.
The replace() and replaceAll() methods are used to return a modified string where the pattern is replaced.

```javascript
var msg = "ball bat";
var n1 = msg.replace(/b/i, "c"); // call bat
var n2 = msg.replaceAll(/b/i, "c"); // call cat
```

The match() and matchAll() methods are used to return the matches when matching a string against a regular expression.

```javascript
var msg = "Hello John";
var n1 = msg.match(/[A-Z]/g); // ["H", "J"]
var n2 = msg.matchAll(/[A-Z]/g); // this returns an iterator
```

The split() method is used to split a string into an array of substrings, and returns the new array.

```javascript
var msg = "Hello John";
var n = msg.replace(/John/i, "Buttler"); // Hello Buttler
var n = msg.split(/\s/); // ["Hello", "John"]
```

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

0 comments on commit a8b1205

Please sign in to comment.