About Lesson
String Methods Using Regex:
a) match()
Finds all matches in a string and returns an array.
JavaScript
let text = "The price is 100 dollars";
let regex = /\d+/; // Matches one or more digits.
let result = text.match(regex);
console.log(result); // Output: ["100"]
b) replace()
Replaces matched text in a string.
JavaScript
let text = "I love cats";
let regex = /cats/;
let replacedText = text.replace(regex, "dogs");
console.log(replacedText); // Output: "I love dogs"
c) test()
Tests if a pattern exists in a string. Returns true or false.
JavaScript
let regex = /hello/;
console.log(regex.test("hello world")); // true
console.log(regex.test("world")); // false