miniApps
2019-11-05 17:35
采纳率: 20%
浏览 800
已采纳

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

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条回答 默认 最新

相关推荐 更多相似问题