最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I do run a regex on a regex in javascript? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 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__: /(?:)/

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());
发布评论

评论列表(0)

  1. 暂无评论