2

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

  1. 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 + '';
  1. 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() + '';
  1. 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.

Community
  • 1
  • 1
  • I'm not sure I understand. Is simple variable submitted by an user?. Especially this line: `in this case I want that if the user puts any number(inside the simpleVariable) it should apply the match` ? Can you tell us exactly what you're trying to achieve, maybe the answer it is different from what you think. – Marcos Casagrande Jun 03 '18 at 18:01
  • @MarcosCasagrande the String should be same as is(spaces, escape sequences) but only the numbers can change.I edited the question too.Hope it is a bit clear now!!! –  Jun 03 '18 at 18:03
  • @Wiktor Stribizew I edited my question –  Jun 03 '18 at 18:11
  • `vsr re='\\d+';` and then `var rx=new RegExp('Hello there\n\t I like ' + re + '\n ' + re + '\n ' + re);`. Do mot compile the pattern that you want to reuse inside the target pattern. Still a dupe. – Wiktor Stribiżew Jun 03 '18 at 18:13
  • @WiktorStribiżew now also this is having some problems.Please have a look at [**this**](https://regex101.com/r/S4WGzr/2).In the input case I want that both the string should match because everything is same except the numbers –  Jun 03 '18 at 18:18
  • 1
    You did not account for a varibale amount of spaces, see https://regex101.com/r/S4WGzr/3 – Wiktor Stribiżew Jun 03 '18 at 18:37
  • @WiktorStribiżew You are a miracle worker Sir. Can you please mention it in answer. This is exactly what I wanted. I want to mark this answer as accepted –  Jun 03 '18 at 18:44
  • Then please edit the question: replace your tries with `\d` with tries with `\\d` and only keep the correct one (no `.exec()`). I will reopen then. – Wiktor Stribiżew Jun 03 '18 at 18:46
  • For sure sir. Will do in a minute just applying for my exact application. –  Jun 03 '18 at 18:47

0 Answers0