Aryan PrajapatKnowledge Contributor
What are the string methods that accept Regular expression
What are the string methods that accept Regular expression
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
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.
var msg = “Hello John”;
var n = msg.search(/John/i); // 6
The replace() and replaceAll() methods are used to return a modified string where the pattern is replaced.
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.
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.
var msg = “Hello John”;
var n = msg.split(/\s/); // [“Hello”, “John”]