javascript - 如何获得一对字符串的左括号和右括号
问题描述
我想在拆分后找到带有索引的左括号和右括号对,例如我提供字符串
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' :[这个类括号的开始索引,类括号的结束索引]}]
谢谢!任何帮助表示赞赏
解决方案
推荐阅读
- r - 修改 flexdashboard 的 shinyauthr
- mysql - mysql中sqlite_master系统表的等价物是什么?
- reactjs - firestore 通过反应获取和显示数据
- opengl - 我应该尽可能使用最新的 GLSL 版本吗?
- ms-access - 比较 SQL 查询中行 SUM 的值
- css - 是否有适用于旧浏览器的 CSS“内容可见性”替代方案/polyfill?
- javascript - Nuxt Vue 中的 Splidejs
- python - 如何使用 PIL(或其他任何东西)将图像动态调整为文本大小?
- integer - 评分软件不断标记答案 - R 中数据的最小值和最大值之间的整数
- discord.py - 如何让我的 discord.py 机器人计算某个人发送的消息量?