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?
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?
Share Improve this question edited Aug 10, 2021 at 17:47 Peter Seliger 13.4k3 gold badges30 silver badges44 bronze badges asked Mar 4, 2017 at 2:05 Chuang FuChuang Fu 3172 gold badges6 silver badges17 bronze badges 2-
2
heh:
eval("/hello\\s{0,1}[-_.]{0,1}world|ls\\b/gim").test('hello world')
– dandavis Commented Mar 4, 2017 at 2:46 -
Since you were given regex syntax (not string syntax), you can't use that directly with the regex constructor. You could use it with
eval()
. – jfriend00 Commented Mar 4, 2017 at 4:07
4 Answers
Reset to default 6The 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
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
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./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; }
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"))