The JavaScript String match() Method
Learn about the match() method in JavaScript for strings.
The match() method is used to search for a specified regular expression pattern inside a string. It returns an array of all the matches found or null if no matches are found.
Here are some examples of using the match() method:
Example 1:
1 | 'Hi Flavio'.match(/avio/) |
Example 2:
1 | 'Test 123123329'.match(/\d+/) |
Example 3:
1 | 'hey'.match(/(hey|ho)/) |
Example 4:
1 | '123s'.match(/^(\d{3})(\w+)$/) |
Example 5:
1 | '123456789'.match(/(\d)+/) |
Example 6:
1 | '123s'.match(/^(\d{3})(?:\s)(\w+)$/) |
Example 7:
1 | '123 s'.match(/^(\d{3})(?:\s)(\w+)$/) |
Example 8:
1 | 'I saw a bear'.match(/\bbear/) |
Example 9:
1 | 'I saw a beard'.match(/\bbear/) |
Example 10:
1 | 'I saw a beard'.match(/\bbear\b/) |
Example 11:
1 | 'cool\_bear'.match(/\bbear\b/) |
To learn more about regular expressions, check out my Regular Expressions tutorial.
tags: [“JavaScript”, “string methods”, “regular expressions”]