简体   繁体   English

分割字符串(大括号除外)

[英]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 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: 您可以使用match并明确显示括号之间各部分的可选匹配:

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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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