I have the following regex pattern and string:
var str="Is this all there is?";
var patt1=/is/gi;
I would like to just extract the main expression is
(without the modifiers) from var patt1
using another regular expression, which we can call var patt2
for arguments sake.
How is this possible to do in vanilla JavaScript?
I have the following regex pattern and string:
var str="Is this all there is?";
var patt1=/is/gi;
I would like to just extract the main expression is
(without the modifiers) from var patt1
using another regular expression, which we can call var patt2
for arguments sake.
How is this possible to do in vanilla JavaScript?
Share Improve this question asked Dec 25, 2012 at 0:52 technojastechnojas 2691 gold badge2 silver badges7 bronze badges3 Answers
Reset to default 11Yes, patt1
is a regex object.
You could get the regex source by patt1.source
.
> console.dir(patt1);
/is/gi
global: true
ignoreCase: true
lastIndex: 0
multiline: false
source: "is"
__proto__: /(?:)/
No need for a regular expression. Try this:
> patt1.source
"is"
I think what you need is this:
var patt1=/[is]/gi;
alert(patt1.test());