converting RegExp to String then back to RegExp

JavascriptRegex

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

If you don't need to store the modifiers, you can use Regexp#source to get the string value, and then convert back using the RegExp constructor.

var regex = /abc/g;
var str = regex.source; // "abc"
var restoreRegex = new RegExp(str, "g");

If you do need to store the modifiers, use a regex to parse the regex:

var regex = /abc/g;
var str = regex.toString(); // "/abc/g"
var parts = /\/(.*)\/(.*)/.exec(str);
var restoredRegex = new RegExp(parts[1], parts[2]);

This will work even if the pattern has a / in it, because .* is greedy, and will advance to the last / in the string.

If performance is a concern, use normal string manipulation using String#lastIndexOf:

var regex = /abc/g;
var str = regex.toString(); // "/abc/g"
var lastSlash = str.lastIndexOf("/");
var restoredRegex = new RegExp(str.slice(1, lastSlash), str.slice(lastSlash + 1));

Solution 2 - Javascript

const regex = /asd/gi;

converting RegExp to String

const obj = {flags: regex.flags, source: regex.source};
const string = JSON.stringify(obj);

then back to RegExp

const obj2 = JSON.parse(string);
const regex2 = new RegExp(obj2.source, obj2.flags);

Requires ES6+.

Solution 3 - Javascript

You can use the following before storage of your regex literal:

(new RegExp(regex)).source

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source

Example:

regex = /asd/

string = (new RegExp(regex)).source
// string is now "asd"

regex = RegExp(string)
// regex has the original value /asd/

Solution 4 - Javascript

let rx = RegExp.apply(RegExp, str.match(/\/(.*)\/(.*)/).slice(1));

A modified version of @PegasusEpsilon answer

Solution 5 - Javascript

StackOverflow saves the day again, thanks @4castle! I wanted to store some regex rules in a JS file, and some in a DB, combine them into an array of objects like so:

module.exports = {
    [SETTINGS.PRODUCTION_ENV]: [

        {
            "key": /<meta name="generator"[\s\S]*?>/gmi,
            "value": "",
            "regex": true
        },

        ...
    ]
}

Then, loop through each environment's objects and apply it to a string of text. This is for a node/lambda project, so I wanted to use ES6. I used @4castle's code, with some destructuring, and I ended up with this:

    let content = body;
    const regexString = replacement.key.toString();
    const regexParts = /\/(.*)\/(.*)/.exec(regexString);
    const {1: source, 2: flags} = regexParts;
    const regex = new RegExp(source, flags);
    content = content.replace(regex, replacement.value);
    
    return content;

Works a treat!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmax pleanerView Question on Stackoverflow
Solution 1 - Javascript4castleView Answer on Stackoverflow
Solution 2 - JavascriptPaul DraperView Answer on Stackoverflow
Solution 3 - JavascriptjedifansView Answer on Stackoverflow
Solution 4 - JavascriptGilad NovikView Answer on Stackoverflow
Solution 5 - JavascriptKingsleyView Answer on Stackoverflow