So I have a RegExp regex = /asd/
I am storing it as a as a key in my key-val store system.
So I say str = String(regex) which returns "/asd/".
Now I need to convert that string back to a RegExp.
So I try: RegExp(str) and I see /\/asd\//
this is not what I want. It is not the same as /asd/
Should I just remove the first and last characters from the string before converting it to regex? That would get me the desired result in this situation, but wouldn't necessarily work if the RegExp had modifiers like /i or /g
Is there a better way to do this?
(new RegExp(regex)).source()help before storage? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…source, but it's not widely supported yet./../xxx, can't contain all the modifiers available at the time of execution. If it can't do that, it's better to have it all available in a hash, or if it applies to all of them (like 'g') then it doesn't matter. Usually, you'd take the path of least resistance, but if you don't know if it works for every thing, it's better to be conservative and slightly basic. The foundation of good code.