多次使用两个字符串之间的正则表达式(Regex everything between two strings multiple times)

编程入门 行业动态 更新时间:2024-10-27 01:23:07
多次使用两个字符串之间的正则表达式(Regex everything between two strings multiple times)

我有一个类似的字符串

[{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}]

我需要的是标签值:P,M,G,GG,XGG。 我试图获得标签和逗号之间的所有内容,但它不起作用

“(标签)(。*)(,)”

I have a String like

[{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}]

What i need is the label values: P, M, G, GG, XGG. i tried to get everything between label and a comma, but its not working

"(label)(.*)(,)"

最满意答案

(?<=label\"\:)\"\w+\"

这样做会

或者如果你真的想把它解析为json

做这个

var arr = []; var jso = [{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}]; console.log(jso.length); for(var i=0;i<jso.length;i++){ arr.push(jso[i].label); } console.log(arr);

正则表达式演示: http : //regex101.com/r/kT9kE6

(?<=label\"\:)\"\w+\"

this will do it

or if you really want to parse it as json

do this

var arr = []; var jso = [{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}]; console.log(jso.length); for(var i=0;i<jso.length;i++){ arr.push(jso[i].label); } console.log(arr);

regex demo : http://regex101.com/r/kT9kE6

更多推荐

本文发布于:2023-08-04 16:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417513.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   两个   正则表达式   Regex   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!