Searching Strings with Regular Expressions
Regular expressions give you expressive pattern-matching power far beyond simple substring searches. JavaScript integrates regex tightly into its string API, so you can search, test, and extract matches with a handful of methods.
String.search()
search(regex) returns the index of the first match, or -1. It is like indexOf but accepts a regex pattern instead of a plain string.
String.match()
match(regex) returns an array of matched results. Without the global flag it returns the first match plus capture groups. With the g flag it returns an array of all matches but no capture groups. Returns null when nothing matches.
String.matchAll()
matchAll(regex) requires a global regex and returns an iterator of all matches, each including capture groups. This is the most powerful extraction method available.
RegExp.test()
/pattern/.test(str) returns a boolean and is the fastest way to validate that a string matches a pattern.