let string = 'mutiFile[{"name":"新建文件夹 (2).zip","ext":".zip","size":1675876,"path":"/static/upload/2022December/ba145698fcc99fd414f0f4ec6ea418e5.zip"}]'; let newString = string.replace(/\[(.*?)\]/g, 'REPLACED'); console.log(newString);
上面的内容会替换掉[]中括号内的字符串
捕获子表达式
let string = 'mutiFile[{"name":"新建文件夹 (2).zip","ext":".zip","size":1675876,"path":"/static/upload/2022December/ba145698fcc99fd414f0f4ec6ea418e5.zip"}]'; let regexp = /\[(.*?)\]/; let match = string.match(regexp); if (match) { console.log(match[1]); }
上面的代码会将字符串 '{"name":"新建文件夹 (2).zip","ext":".zip","size":1675876,"path":"/static/upload/2022December/ba145698fcc99fd414f0f4ec6ea418e5.zip"}'
输出到控制台。
同样的,注意:上面的正则表达式只能匹配一对中括号,如果字符串中包含多对中括号,则只能匹配第一对中括号内的内容。
标签:string,zip,正则表达式,javascript,let,中括号,字符串,match From: https://www.cnblogs.com/taoshihan/p/17006099.html