0

I am using a regex as shown below

const regex = /^.*?\*AUY:(.*?)$/gmi;

I am trying to pass the string AUY as a variable . I have tried doing the following but it gives an error of incorrect Regular Expression

var strreg="^.*?\*"+ID+":(.*?)$";
const regex = new RegExp(strreg,"gmi");
Bilal Hussain
  • 191
  • 5
  • 18
  • 2
    The issue is that you need to use `\\*`, not `\*`, and the error is related to the fact that your expression tries to quantify a quantifier (`*??`). – Wiktor Stribiżew Nov 17 '16 at 13:38
  • Your problem has nothing to do with passing the `ID` in as a variable - you're doing that bit right. Your original regex had the same problem. `Uncaught SyntaxError: Invalid regular expression: /^.*?*AUY:(.*?)$/: Nothing to repeat` – Jamiec Nov 17 '16 at 13:40
  • @Jamiec but if I try to use it without variable it runs good. – Bilal Hussain Nov 17 '16 at 13:44
  • @WiktorStribiżew tired doing that too but it didn't helped. – Bilal Hussain Nov 17 '16 at 13:45
  • Your original used literal regex syntax (starts/ends with `/`) and with that you dont need to escape characters. When you change to using a string regex and the constructor you need to escape the string. Check the linked duplicate for more info. – Jamiec Nov 17 '16 at 13:46
  • *It didn't helped* - Really? Did you try `var strreg="^.*?\\*"+ID+":(.*?)$";`? Well, let alone you may write it as `var strreg="^[^*]*\\*"+ID+":(.*)";` – Wiktor Stribiżew Nov 17 '16 at 13:47
  • Yup @WiktorStribiżew got that . Thank You – Bilal Hussain Nov 17 '16 at 13:47

0 Answers0