Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question 155: add other string methods that accept RegExp #293

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading