Chuang Fu
Chuang Fu

Reputation: 317

How does one convert a stringified regex literal back into a regular expression?

For example I got a string in client from the server:

"/hello\s{0,1}[-_.]{0,1}world|ls\b/gim"

In client I wants to convert this string into an regex object. I tried

new RegExp("/hello\s{0,1}[-_.]{0,1}world|ls\b/gim")

But this will not work, the returned object is

/\/hellos{0,1}[-_.]{0,1}world|ls\/gim/

To summarize: What I want is:

/hello\s{0,1}[-_.]{0,1}world|ls\b/gim.test('hello world') //true (correct behavior)

However this is not working:

new RegExp("/hello\s{0,1}[-_.]{0,1}world|ls\b/gim").test('hello world') //false

What's the correct way to doing this?

Upvotes: 2

Views: 5163

Answers (4)

gyre
gyre

Reputation: 16777

The RegExp constructor takes two arguments. The first is the literal source/pattern to match (essentially the stuff between the outer / in a regex literal); the second is the flags to be set on that expression (e.g. gim in your example). I defined a helper function for you below that converts strings in your format to regular expressions. Ironically, I ended up using another regex to do so.

function regexFromString (string) {
    const match = /^([^/]*)(\/([a-z]*))?$/.exec(string)
    if(match.length <= 3)
        return new RegExp(match[1])
    return new RegExp(match[1], match[3])
}

var string = '/hello\\s{0,1}[-_.]{0,1}world|ls\\b/gim'

var regex = regexFromString(string)

console.log(regex instanceof RegExp) //=> true
console.log(regex)
console.log(regex.test('hello world')) //=> true

Upvotes: 6

Constantin Schreiber
Constantin Schreiber

Reputation: 71

If a server provides regex to the client, the following Regex matches another Regex and separates body from flags to properly reconstruct it.

function regexFromString(string) {
    const matchFlags = /^(.*)(\/([a-z]*))$/.exec(string)
    if (matchFlags && matchFlags.length >= 4)
        return new RegExp(matchFlags[1], matchFlags[3])

    return new RegExp(string)
}

var regex = regexFromString("^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})$/i")
console.log(regex.test("NtSbDeB1XxX"))

Upvotes: 0

Peter Seliger
Peter Seliger

Reputation: 13377

Late answer

An approach which is based on regex itself would capture from any provided stringified regex literal the expession's body and its optional flags part.

The regex itself could look like this ... /^\/(?<body>.*)\/(?<flags>[gimsuy]*)$/ ... and a failsafe approach which uses this regex then might look like the beneath one ...

// see ... [https://regex101.com/r/Ek881d/2]

function regexFromString(str) {
  const {
    body,
    flags,
  } = String(str)
    .match(/^\/(?<body>.*)\/(?<flags>[gimsuy]*)$/)
    ?.groups || {};

  return RegExp(body, (body && flags));
}

console.log(
  "regexFromString(String(/hello\s{0,1}[-_.]{0,1}world|ls\b/gim)).test('hello world') ? ",

  regexFromString(
    String(/hello\s{0,1}[-_.]{0,1}world|ls\b/gim)
  ).test('hello world')
);
console.log(
  "regexFromString('') ...",
  regexFromString('')
);

console.log(
  regexFromString('/foo/'),
  'this isfoook'.split(regexFromString('/foo/'))
);
console.log(
  regexFromString('/foo\\/bar/ig'),
  'this isfoo/barok'.split(regexFromString('/foo\\/bar/ig'))
);
console.log(
  regexFromString('/foo/bar/ig'),
  'this isfoo/barok'.split(regexFromString('/foo/bar/ig'))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Upvotes: 0

Robert
Robert

Reputation: 10390

Using the RegExp constructor is a little different, here is what I believe you are looking for:

var x = new RegExp('hello\\s{0,1}[-_.]{0,1}world|ls\\b', 'gim').test("hello world");

console.log(x);

returns true

Upvotes: 1

Related Questions