0

I created a RegExp that works as expected however I can't get it to work in a string format

var regex = new RegExp(/IN1\|2.*\r/g);

The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?

I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"

Below is a text in case anyone wants to try:

IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r

Thank you.

tym
  • 73
  • 1
  • 6
  • 1
    Main point is that a backslash that forms a regex escape must be a literal backslash. `new RegExp("IN1\\|" + lineNumber + ".*\\r", "g")` or `new RegExp("IN1\\|" + lineNumber + ".*\r", "g")`. – Wiktor Stribiżew Jun 12 '20 at 14:40
  • @WiktorStribiżew ahh, I don't know how I missed that, thank you! – tym Jun 12 '20 at 14:47

0 Answers0