-1

I’m sending a regex expression via JSON from my Ember front end to my Node server. When the regex expression is sent it is converted to a string. This is an example of what node receives:

{"find":{"name":"/^sp101/"},"sort":{"name":"1"},"limit":5}

In this example how do I convert find[‘name’] from a string to a regex? i.e. I want to extract /^sp101/ from find[‘name’] instead of “/^sp101/“

When I pass the string "/^sp101/" to a RegExp constructor it converts it to //^sp19// which I do not want.

Is there a way to accomplish this without using string manipulation? i.e. slicing off the first and last "/" before passing it to the constructor.

CBK
  • 141
  • 1
  • 6
  • 1
    Related: https://stackoverflow.com/questions/38160746/how-to-convert-a-regular-expression-to-a-string-literal-and-back-again – Steven Lambert Jul 07 '16 at 19:08

1 Answers1

0

Use eval

var s = "/^sp101/"

eval(s) -> returns /^sp101/
CBK
  • 141
  • 1
  • 6