简体   繁体   中英

spliting string except in curly brackets

I'm trying to split a string on "," but I don't want to split if "," is in "{}" . There's no nested braces.

here is the string I want to split :

EmbiBISupplierKPIResult_EditArea00001,EmbiBISupplierKPISearch,partyId=E10021&economicAreaPartyIds={DPP_20246, DPP_14726}&economicId={DPP_20246, DPP_14726}&requestedDateSearchType=ACTUAL_WEEK

Here is the result I expect :

EmbiBISupplierKPIResult_EditArea00001
EmbiBISupplierKPISearch
partyId=E10021&economicAreaPartyIds={DPP_20246, DPP_14726}&economicId={DPP_20246, DPP_14726}&requestedDateSearchType=ACTUAL_WEEK

Is there a regex to do this ?

You can use match and be explicit about the optional matching of parts between braces:

var parts = s.match(/(\{[^{}]*\}|[^,{}]+)+/g)

 var s = 'EmbiBISupplierKPIResult_EditArea00001,EmbiBISupplierKPISearch,partyId=E10021&economicAreaPartyIds={DPP_20246, DPP_14726}&economicId={DPP_20246, DPP_14726}&requestedDateSearchType=ACTUAL_WEEK'; var parts = s.match(/(\\{[^{}]*\\}|[^,{}]+)+/g) document.querySelector('pre').innerHTML = JSON.stringify(parts,null,'\\t'); 
 <pre id=result></pre> 

You can use the following:

,(?=(?:[^{}]*{[^{}]*})*[^{}]*$)

See DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM