JavaScript String match() method

The JavaScript string match() method determines a specified regular expression in a given string and returns that regular expression if a match occurs.

Note: To get all the matches we have to use a global modifier.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
string.match(regexp)
string.match(regexp)
string.match(regexp)

Parameters:
regexp: It represents the regular expression to be searched.

Return:
Matched regular expression.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var a ="Best tutorial website: w3schools";
document.write(a.match('co'));
document.write("</br>");
document.writeln(a.match(/web/));
document.write("</br>");
document.writeln(a.match(/web/i));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var a ="Best tutorial website: w3schools"; document.write(a.match('co')); document.write("</br>"); document.writeln(a.match(/web/)); document.write("</br>"); document.writeln(a.match(/web/i)); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
var a ="Best tutorial website: w3schools";
document.write(a.match('co'));
document.write("</br>");
document.writeln(a.match(/web/));
document.write("</br>");
document.writeln(a.match(/web/i));
</script>
</body>
</html>