I am trying to create a regular expression which contains the nested regular expression mentioned as below:
var simpleVariable = 'Hello there\n\t I like 2345\n 4567\n 1298';
var re = new RegExp(simpleVariable, 'g');
//To do my code in here
However, in this case I want that if the user puts any set of number( other than which are inside the simpleVariable) it should apply the match i.e. Valid matches(which are required):
Hello there
I like 2345
4567
1298
Hello there
I like <any number>
<any number>
<any number>
Things which I tried
- Firstly I tried to append the regex to the String as such like:
var re = new Regexp('\d+', 'g');
var result = 'Hello there\n\t I like ' + re + '\n ' + re + '\n ' + re + '';
- Secondly I tried to apply compile() or exec() methods(Since I am a complete beginner in javascript and node.js)
var re = new Regexp('\d+', 'g');
var result = 'Hello there\n\t I like ' + re.exec() + '\n ' + re.exec() + '\n ' + re.exec() + '';
- I referred This link. In there
Then()
function they are nesting a regex("([^"]*)"
) inside the string but I was not able to understand it and it didn't helped in sorting out my problem.Also I need to pass this regex to
replace-in-file
module and that is one major challenge too.
Since I am new to node.js and javascript I am not able to get the exact idea is to how to solve this particular problem. Any help is welcomed.