首页 > 解决方案 > 如何获得一对字符串的左括号和右括号

问题描述

我想在拆分后找到带有索引的左括号和右括号对,例如我提供字符串

var str="class User{      constructor(name=null){ if(name !=null){ this.name=name; }else{  
this.name=`Created by Test`; } } sayHello(){     
 console.log(`Welcome to Testing`); } 
showName(){ console.log(`This is ${this.name} this side`); 
if(true){  }else{  } } } function testing(){};    
 class Details { constructor(name=null){ if(name !=null){ this.name=name; }else{ 
 this.name=`Created by Tester`; } }
 sayName(){ console.log(`Welcome to Test2`); } }"; 

str=str.split(' ').filter(n => n).join(' ');  //replaces extra spaces with none
var strArr= str.split(" "); //by using this array of string find index pair, 

我必须找到每个类的开闭对数组,例如 [{'class':'User','index_pair':[11,closure index of class brackets]},{'class':'Details','index_pair' :[这个类括号的开始索引,类括号的结束索引]}]

谢谢!任何帮助表示赞赏

标签: javascript

解决方案


推荐阅读