miniApps
miniApps
2019-11-05 17:35

js正则匹配字符串中的完整Class类,大花括号内可能嵌套有花括号!

20
  • 正则表达式
  • javascript

class类名是变量,所以要用new RegExp

这是我写的一部分,但匹配不全:

    var str = 'asfsdf class Bop { public double breadth; public double height; static void get(){ var test = asas; var ssasd = function(){} } static void set(){} } class Boxtester { static void Main(string[] args) { Box Box1 = new Box(); Box Box2 = new Box(); double volume = 0.0; // Box1 详述 Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; } }';

        //要匹配出来的字段是
        //class Bop { public double breadth; public double height; static void get(){ var test = asas; var ssasd = function(){} } static void set(){} }

    var word = 'Bop';        
    var res = new RegExp("class\\s+"+word+"\\s*\\{(\\s+(.+?)\\s+)*\\}","g");
    console.log(res.exec(str))

求教各位大神!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答