7

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?

3 Answers 3

11

Yes, 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__: /(?:)/
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - Accepting this answer only because I saw it first - otherwise there was not much difference between them two :)
9

No need for a regular expression. Try this:

> patt1.source
"is"

1 Comment

+1 - Wow! I had no idea this method existed on the regex object.
0

I think what you need is this:

 var patt1=/[is]/gi;
 alert(patt1.test());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.