简体   繁体   English

Javascript 正则表达式按句点分割字符串,不在双引号中

[英]Javascript regular expression split string by periods not in double quotes

I have the following regular expression /\.(??[^"]*"(:?(:?[^"]*"){2})*[^"]*$)(.=[^\.]+)/g to split strings by periods using javascript String.split function, if the period is not within double quotes "" and also if the period does not occur at the end of the string.我有以下正则表达式/\.(??[^"]*"(:?(:?[^"]*"){2})*[^"]*$)(.=[^\.]+)/g使用 javascript String.split function 按句点拆分字符串,如果句点不在双引号""内,并且句点不在字符串末尾。

It seems to work well for the simple cases, like hello."world. works".well.它似乎适用于简单的情况,例如hello."world. works".well. yields ['hello ', '"world. works"', 'well.'] .产生['hello ', '"world. works"', 'well.']

But I have this complex example 'test."One.word".A short sentence." no split. " no split.".'但我有这个复杂的例子'test."One.word".A short sentence." no split. " no split.".' where it splits incorrectly to ['test."One ', 'word".A short sentence." no split', ' " no split.".']它错误地拆分为['test."One ', 'word".A short sentence." no split', ' " no split.".'] ['test."One ', 'word".A short sentence." no split', ' " no split.".']

I've run out of ideas how to fix this.我已经没有办法解决这个问题了。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Use利用

/(?:"[^"]*"|[^.])+(?:\.+$)?/g

See regex proof .请参阅正则表达式证明

JavaScript code : JavaScript 代码

 const regex = /(?:"[^"]*"|[^.])+(?:\.+$)?/g; const str = `test."One.word".A short sentence." no split. " no split.".`; console.log(str.match(regex));

EXPLANATION解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    [^.]                     any character except: '.'
--------------------------------------------------------------------------------
  )+                       end of grouping
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional):
--------------------------------------------------------------------------------
    \.+                      '.' (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )?                       end of grouping

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

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