# 正则如何匹配多个分组？

``````var a='+12+{123}+{123}+12+12+{123}'
a.replace(/\+([\d]{2})|\+{([\d]{3})}/gi,(x,y,z)=>{
console.log(x);
console.log(y);
console.log(z);
})
``````

2 个回答

``````var a='+12+{123}+{123}+12+12+{123}'
a.replace(/(\d{2,3})/gi,(x,y,z)=>{
console.log(`x:\${x}`);
console.log(`y:\${y}`);
console.log(`z:\${z}`);
})``````

``````var str="+12+{123}+{123}+12+{123}+12+{123}+{123,098}+123+6787+543";
var result=str.match(/\+(\d{2}\b|{\d{3}})/g).map(function(matchStr){
return matchStr.match(/\d+/);
});``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进
{"ft_lang_label":"__label__zh","ft_lang_prob":0.8992213,"math_prob":0.9997045,"size":361,"snap":"2022-05-2022-21","text_gpt3_token_len":254,"char_repetition_ratio":0.12605043,"word_repetition_ratio":0.0,"special_character_ratio":0.41274238,"punctuation_ratio":0.14285715,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9957067,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-20T03:35:06Z\",\"WARC-Record-ID\":\"<urn:uuid:6cb28d32-3d28-4b57-bd0c-180c307c572a>\",\"Content-Length\":\"41944\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5ee8bc36-cdbe-47ca-9e4b-23c856fc9906>\",\"WARC-Concurrent-To\":\"<urn:uuid:ccf3d2dd-49f9-49e3-8795-c971ecd4d31b>\",\"WARC-IP-Address\":\"103.61.39.86\",\"WARC-Target-URI\":\"https://segmentfault.com/q/1010000012032217\",\"WARC-Payload-Digest\":\"sha1:SAVWF4UE7UIZAHHLQROVZJ2ITOQCTBQ7\",\"WARC-Block-Digest\":\"sha1:A7HIH3VULXGGGGNMMYZCQ4PNAOY7QC3S\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662531352.50_warc_CC-MAIN-20220520030533-20220520060533-00163.warc.gz\"}"}