javascript中有一字符串:
"other oder;var vppp=0,vuuu='AAA',v$ddd,vkkk=8,aaaaa=ppp,bbbb=5,bbbbkk;other oder;var iii=8,ibk;other oder;"
想提取由var定义的所有变量名称,最终结果应该为:
['vppp','vuuu', 'v$ddd', 'vkkk', 'aaaaa', 'bbbb', 'bbbbkk','iii','ibk']
我是分两步(两个正则)进行的:
"other oder;var vppp=0,vuuu='AAA',v$ddd,vkkk=8,aaaaa=ppp,bbbb=5,bbbbkk;other oder;var iii=8,ibk;other oder;"
.match(/(?<=var\s).*?(?=;)/g).forEach((item)=>{
p=item.match(/(?<=^|,)([a-zA-Z_$][a-zA-Z_$0-9]*)(?=,|=|$)/g);
console.log(p);
})
现在是想只用一个正则就搞定它:
"other oder;var vppp=0,vuuu='AAA',v$ddd,vkkk=8,aaaaa=ppp,bbbb=5,bbbbkk;other oder;var iii=8,ibk;other oder;"
.match(/(?<=var\s)????????(?=;)/g)
这个正则怎么写?
"other oder;var vppp=0,vuuu='AAA',v$ddd,vkkk=8,aaaaa=ppp,bbbb=5,bbbbkk;other oder;var iii=8,ibk;other oder;"
想提取由var定义的所有变量名称,最终结果应该为:
['vppp','vuuu', 'v$ddd', 'vkkk', 'aaaaa', 'bbbb', 'bbbbkk','iii','ibk']
我是分两步(两个正则)进行的:
"other oder;var vppp=0,vuuu='AAA',v$ddd,vkkk=8,aaaaa=ppp,bbbb=5,bbbbkk;other oder;var iii=8,ibk;other oder;"
.match(/(?<=var\s).*?(?=;)/g).forEach((item)=>{
p=item.match(/(?<=^|,)([a-zA-Z_$][a-zA-Z_$0-9]*)(?=,|=|$)/g);
console.log(p);
})
现在是想只用一个正则就搞定它:
"other oder;var vppp=0,vuuu='AAA',v$ddd,vkkk=8,aaaaa=ppp,bbbb=5,bbbbkk;other oder;var iii=8,ibk;other oder;"
.match(/(?<=var\s)????????(?=;)/g)
这个正则怎么写?