我不太確定這是否可能,所以我求助于你。我想找一個正則表達式,將挑選所有逗號以外的引號集。例如:'foo' => 'bar','foofoo' => 'bar,bar'這將在第1行之后選擇單個逗號。'bar',我不關心單引號還是雙引號。有人有什么想法嗎?我覺得這是有可能的,但我的判斷力太弱了。在引號之外選擇逗號的Regex
3 回答

拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
,(?=(?:[^"]*"[^"]*")*[^"]*$)
或
"[^"]*"(*SKIP)(*F)|,
"[^"]*"
buz,"bar,foo"
"bar,foo"
(*SKIP)(*F)
|
,
|
buz
,(?!(?:[^"]*"[^"]*")*[^"]*$)

紫衣仙女
TA貢獻1839條經驗 獲得超15個贊
function splitNotStrings(str){ var parse=[], inString=false, escape=0, end=0 for(var i=0, c; c=str[i]; i++){ // looping over the characters in str if(c==='\\'){ escape^=1; continue} // 1 when odd number of consecutive \ if(c===','){ if(!inString){ parse.push(str.slice(end, i)) end=i+1 } } else if(splitNotStrings.quotes.indexOf(c)>-1 && !escape){ if(c===inString) inString=false else if(!inString) inString=c } escape=0 } // now we finished parsing, strings should be closed if(inString) throw SyntaxError('expected matching '+inString) if(end<i) parse.push(str.slice(end, i)) return parse}splitNotStrings.quotes="'\"" // add other (symmetrical) quotes here
- 3 回答
- 0 關注
- 675 瀏覽
添加回答
舉報
0/150
提交
取消