Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting RegExp to String then back to RegExp

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?

like image 745
max pleaner Avatar asked Aug 25 '16 20:08

max pleaner


People also ask

What is $1 in regex replace?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.

How do you make a string in regex?

Example : ^\d{3} will match with patterns like "901" in "901-333-". It tells the computer that the match must occur at the end of the string or before \n at the end of the line or string. Example : -\d{3}$ will match with patterns like "-333" in "-901-333". A character class matches any one of a set of characters.

What does regex (? S match?

i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.

How do you convert string to regex in JavaScript?

Javascript. <script>. const str = "Geeks for Geeks"; // Input from User. const regex = prompt ("Enter RegExp"); // Conversion from string to RegExp. const reg = new RegExp (regex, "g"); // The match fn returns the array of strings. // That match to RegExp.

How do you use replaceAll in regex?

The main use case for replaceAll is replacing all occurences of a string. The regexp.exec (str) method returns a match for regexp in the string str. Unlike previous methods, it’s called on a regexp, not on a string. It behaves differently depending on whether the regexp has flag g.

What is a REGEXP in JavaScript?

Regular expressions (RegExp) are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. There are two ways to construct a regular expression in JavaScript . 1. Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows.

How do you match a string in regex?

The regexp.exec (str) method returns a match for regexp in the string str. Unlike previous methods, it’s called on a regexp, not on a string. It behaves differently depending on whether the regexp has flag g. If there’s no g, then regexp.exec (str) returns the first match exactly as str.match (regexp).


Video Answer


3 Answers

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));
like image 169
4castle Avatar answered Oct 10 '22 21:10

4castle


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+.

like image 15
Paul Draper Avatar answered Oct 10 '22 23:10

Paul Draper


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/
like image 2
jedifans Avatar answered Oct 10 '22 23:10

jedifans