
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Looping over matches with JavaScript regular expressions
Following is the code for looping over regular expression matches in javaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result, .sample { font-size: 18px; color: blueviolet; font-weight: 500; } .sample { color: red; } </style> </head> <body> <h1>Looping over regex matches</h1> <div class="sample"> There is a cat lying besides the cricket bat and chasing the rat </div> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to match words ending with 'at' in the above sentence</h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let sampleEle = document.querySelector(".sample"); let text = sampleEle.innerHTML; BtnEle.addEventListener("click", () => { text.match(/at/g).forEach((element) => { resEle.innerHTML += 'Found a match ending with "at" <br>'; }); }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Articles
- JavaScript Regular Expressions
- Getting the list of all the matches Java regular expressions
- Lookbehind Assertions JavaScript Regular Expressions
- What are regular expressions in JavaScript?
- Unicode Property Escapes JavaScript Regular Expressions
- Named capture groups JavaScript Regular Expressions
- How to write JavaScript Regular Expression for multiple matches?
- How can I simplify the usage of Regular Expressions with JavaScript?
- Validate Phone with Java Regular Expressions
- Explain ‘dotAll’ flag for regular expressions in JavaScript
- Search and Replace with Java regular expressions
- How to use regular expressions with TestNG?
- Assign new value to item in an array if it matches another item without looping in JavaScript?
- Validate city and state with Java Regular Expressions
- Validate the ZIP code with Java Regular expressions

Advertisements