正则表达式分裂“,”和“

时间:2015-01-14 07:09:13

标签: regex

这是我到目前为止所提出的

 var split_string = string_a.split(/(?:"|",")/);

然而

var string_a ='"ldfssdf","mom , ddd"';

它没有在数组中给出正确的结果 我基本上想要的是这个

 string_a = ["ldfssdf", "mom, ddd"];

2 个答案:

答案 0 :(得分:1)

,(?=(?:[^"]*"[^"]*")*[^"]*$)

由此分开。参见演示。

https://regex101.com/r/fA6wE2/11

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ,                        ','
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                         times (matching the most amount
                         possible)):
--------------------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                           times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
      "                        '"'
--------------------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                           times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
      "                        '"'
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                         times (matching the most amount
                         possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                         the string
--------------------------------------------------------------------------------
  )                        end of look-ahead

答案 1 :(得分:0)

这是你想要做的。

点击此链接:http://jsfiddle.net/duongtuanluc/q8z7fkgx/

检查此链接:http://jsfiddle.net/duongtuanluc/q8z7fkgx/1/(如果包含空值"")

var string_a ='"ldfssdf","mom , ddd"';
var rgx = /(\"[\s\S]+?\")/g;
var arr = string_a.match(rgx);
console.log(arr);

enter image description here