- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to put variable in regular expression match with JavaScript?
You can use match() with passing the variable name. The syntax is as follows −
console.log(yourVariableName.match(yourMatchingVariableName));
Example
var senetence = 'My Name is John'; console.log("The actual value="); console.log(senetence); var matchWord = 'John'; console.log("The matching value="); console.log(matchWord); var matchRegularExpression = new RegExp(matchWord, 'g' ); console.log(senetence.match(matchWord));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo107.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo107.js The actual value= My Name is John The matching value= John
Advertisements