由括号外的逗号分隔的jQuery / JavaScript [英] jQuery/JavaScript Split String by Commas that are Outside of Parenthesis

查看:50
本文介绍了由括号外的逗号分隔的jQuery / JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery& JavaScript !!



我有一个字符串,例如

  cubic-bezier(0.25,0,0.25,1),easy-in,ease-out,linear

..我想把它拆分成这个数组:

  //(以下没有前导或尾随空格: )

数组[4]
0:cubic-bezier(0.25,0,0.25,1)
1:轻松进入
2:缓出
3:线性

..但我得到:

 数组[7] 
0:cubic-bezier(0.25
1:0
2:0.25
3 :1)
4:缓入
5:缓出
6:线性

我尝试了十几种解决方案,但却发现我尝试它的任何方式到目前为止都无效。以下是我以前的一些尝试:

  var myString =cubic-bezier(0.25,0,0.25,1),舒适,缓和,线性; 

myString.split(,(?![^ \(\)] *>));

myString.split('(?:\(。*?\))|(,)');

myString.split(,(?= [^ \]] *(?:\ [| $)));

myString.split(,(?![^()] *(?: \([^()] * \))?\)));

myString.split('/,(?![^()] *(?:\([^()] * \))?\)/ g');

myString.split(,(?=(([^'] *'){2})* [^'] * $)(?=(([^ \] * \){2})* [^ \] * $)(?![^()] * \\)));

我熟悉基本的正则表达式规则,例如捕获组和选择器,但我仍然对前瞻或我如何解决当前情况感到困惑。

解决方案

匹配任何不在括号内的逗号



  var myString =cubic-bezier(0.25,0,0.25,1),ease-in,ease-out,linear; var parts = myString.split(/ \, \s /)?([^ \(] * \)!);执行console.log(份) 

  / \,\??(?![^ \(] * \))/ 




  • \,匹配字符字面意思

  • \?匹配任何空格字符。量词匹配0到1次

  • (?![^ \(] * \)) Negative Lookahead断言正则表达式与下面列表中不存在的单个字符不匹配

  • [^ \(] 量词。在零和无限次之间匹配,尽可能多次

  • \(匹配字符字面意思

  • \)匹配字符字面意思


I am using jQuery & JavaScript!!

I have a string, for example

"cubic-bezier(0.25, 0, 0.25, 1), ease-in, ease-out, linear"

..and I want to split it into this array:

// (the following has no leading or trailing spaces:)

Array[4]
  0: cubic-bezier(0.25, 0, 0.25, 1)
  1: ease-in
  2: ease-out
  3: linear

..but instead I get:

Array[7]
  0: cubic-bezier(0.25
  1: 0
  2: 0.25
  3: 1)
  4: ease-in
  5: ease-out
  6: linear

I have attempted a dozen solutions, only to find that any way that I try it doesn't work thus far. Here are a few of my previous attempts:

var myString = "cubic-bezier(0.25, 0, 0.25, 1), ease-in, ease-out, linear";

myString.split(",(?![^\(\)]*>)");

myString.split('(?:\(.*?\))|(,)');

myString.split(",(?=[^\]]*(?:\[|$))");

myString.split(",(?![^()]*(?:\([^()]*\))?\))");

myString.split('/,(?![^()]*(?:\([^()]*\))?\)/g');

myString.split(",(?=(([^']*'){2})*[^']*$)(?=(([^\"]*\"){2})*[^\"]*$)(?![^()]*\\))");

I am familiar with basic regex rules, such as capture groups and selectors, but am still confused on lookaheads or how I would solve my current situation.

解决方案

Match any comma not inside parentheses

var myString = "cubic-bezier(0.25, 0, 0.25, 1), ease-in, ease-out, linear";

var parts    = myString.split(/\,\s?(?![^\(]*\))/);

console.log(parts)

/\,\s?(?![^\(]*\))/

  • \, matches the character , literally
  • \s? matches any whitespace character. The ? quantifier matches between zero and one times
  • (?![^\(]*\)) Negative Lookahead asserts that the regex does not match a single character not present in the list below
  • [^\(] Quantifier. Matches between zero and unlimited times, as many times as possible
  • \( matches the character ( literally
  • \) matches the character ) literally

这篇关于由括号外的逗号分隔的jQuery / JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆