JS Coding Questions Logo
JS Coding Questions
#156💼 Interview💻 Code

What are the string methods that accept Regular expression

Advertisement

728x90

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

The search() method uses an expression to search for a match, and returns the position of the match.

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

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

javascript
1var msg = "ball bat";
2
3var n1 = msg.replace(/b/i, "c"); // call bat
4
5var 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
1var msg = "Hello John";
2
3var n1 = msg.match(/[A-Z]/g); // ["H", "J"]
4
5var 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
1var msg = "Hello John";
2
3var n = msg.split(/\s/); // ["Hello", "John"]

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 71

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
156of476
What are the string methods that accept Regular expression | JSCodingQuestions.com