2 Example(s) of JavaScript match function


Description :

JavaScript match() function return an array by searching a string for a match against a regular expression.. See the code snippet:


JavaScript match function Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
  <button onclick="matchString()">Show the matched string</button>
<p id="sample"></p>
<script>
function matchString() {
    var str = "I love learnKode that keeps my learning of code so easy."; 
    var result = str.match(/earn/g);
    document.getElementById("sample").innerHTML = result;
}
</script>      
</body>
</html>

Output

Description :

JavaScript match function with regular expression:


JavaScript match function Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<script>
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
var result = str.match(regexp);
document.write(result);
</script>
     

</body>
</html>

Output