- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- Regular expression to match numbers only in JavaScript?
- How to match parentheses in Python regular expression?
- How to write Python regular expression to match with file extension?
- How to use regular expression in Java to pattern match?
- How to match a word in python using Regular Expression?
- How to match nonword characters in Python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- How to match only digits in Python using Regular Expression?
- How to match a regular expression against a string?
- How to match digits using Java Regular Expression (RegEx)
- How to match a nonwhitespace character in python using Regular Expression?
- How to match only non-digits in Python using Regular Expression?
- How to match a single character in python using Regular Expression?
- PHP – Match regular expression using mb_ereg_match()
- How to match non-digits using Java Regular Expression (RegEx)

Advertisements