0

I created regular expression for nodejs aggaregate query. But can not create it.

Here, is my code.

    var device = "HPM220";
    var expression = /^[device].*/i;
    console.log(expression);

My device name is "HPM220". but my output is /^[device].*/i. It is wrong output.

The output should be /^[HPM220].*/i

What mistake i did here?

Harish Mahajan
  • 3,254
  • 4
  • 27
  • 49
  • Pretty sure `[HPM220]` is not what you want, though - square brackets denote a character set – CertainPerformance Nov 05 '19 at 06:44
  • @CertainPerformance yes right. – Harish Mahajan Nov 05 '19 at 06:55
  • I need my device name there. I tried with this `"/^["+device+"].*/i"` i got my output but when i pass it to my aggregate query it does not works. – Harish Mahajan Nov 05 '19 at 06:58
  • Like I said, the RE you're creating contains a *character set* of those characters, which is almost certainly not what you want. Figure out the regex you need, and then you can figure out how to concatenate properly. You also shouldn't be passing the `/` delimiters or flags to `new RegExp` – CertainPerformance Nov 05 '19 at 06:59

0 Answers0