

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Regex - reusing patterns to capture groups in JavaScript?
For this, use regular expression with digit along with $.
Example
var groupValues1 = "10 10 10"; var groupValues2 = "10 10 10 10"; var groupValues3 = "10 10"; var regularExpression = /^(\d+)(\s)\1\2\1$/; var isValidGroup1 = regularExpression.test(groupValues1); var isValidGroup2 = regularExpression.test(groupValues2); var isValidGroup3 = regularExpression.test(groupValues3); if(isValidGroup1==true) console.log("This is a valid group="+groupValues1); else console.log("This is not a valid group="+groupValues1); if(isValidGroup2==true) console.log("This is a valid group="+groupValues2); else console.log("This is not a valid group="+groupValues2); if(isValidGroup3==true) console.log("This is a valid group="+groupValues3); else console.log("This is not a valid group="+groupValues3);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo188.js.
Output
The below output matches only groups 3. This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo188.js This is a valid group=10 10 10 This is not a valid group=10 10 10 10 This is not a valid group=10 10
- Related Questions & Answers
- Named capture groups JavaScript Regular Expressions
- Regex named groups in Java
- Named Capturing groups in Java Regex
- Validate IPv4 address using ReGex patterns in C++
- Validate IPv6 address using ReGex patterns in C++
- How to get the number of capture groups in Python regular expression?
- Capturing groups and back references in Java Regex
- Regex capturing groups and back references in Java
- How to check multiple regex patterns against an input? Using Java.
- How to capture multiple matches in the same line in Java regex
- How to Match patterns and strings using the RegEx module in Python
- How do I use capturing groups in Java Regex?
- List some design patterns in javascript?
- Similar string groups in JavaScript
- Reduce an array to groups in JavaScript
Advertisements