Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript匹配拆分文本_Javascript_Regex - Fatal编程技术网

使用javascript匹配拆分文本

使用javascript匹配拆分文本,javascript,regex,Javascript,Regex,对于以下代码 var str = "I left the United States with my eyes full of tears! I knew I would miss my American friends very much.All the best to you"; var re = new RegExp("[^\.\?!]*(?:[\.\?!]+|\s$)", "g"); var myArray = str.match(re); 这就是我得到的结果 myArray[0] =

对于以下代码

var str = "I left the United States with my eyes full of tears! I knew I would miss my American friends very much.All the best to you";
var re = new RegExp("[^\.\?!]*(?:[\.\?!]+|\s$)", "g");
var myArray = str.match(re);
这就是我得到的结果

myArray[0] = "I left the United States with my eyes full of tears!"
myArray[1] = " I knew I would miss my American friends very much."
我想在regex中再添加一个条件,这样只有在存在 标点符号后面的空格(?或.或!)

我这样做是为了上面的结果是

myArray[0] = "I left the United States with my eyes full of tears!"
myArray[1] = " I knew I would miss my American friends very much.All the best to you "
myArray[2] = ""
应该有用

它将匹配以下任意字符序列:

  • 后跟标点符号,标点符号本身后跟空格或字符串结尾,或
  • 后跟字符串的末尾
通过使用不情愿的量词
+?
,它可以找到尽可能短的序列(=单句)

在JavaScript中:

result = subject.match(/.+?([!?.](?= |$)|$)/g);
编辑:

为了避免“空格/单字母或多位数/点”上的正则表达式拆分,您可以使用:

result = subject.match(/( \d+\.| [^\W\d_]\.|.)+?([!?.](?= |$)|$)/g);
这将分裂

我带着我的眼睛离开了美国 满是泪水!23我知道我会错过的 我非常喜欢我的美国朋友。一、全部 祝你一切顺利

进入

它所做的不是简单地匹配任何字符直到找到一个点,而是:

  • 首先尝试匹配空格、数字和点
  • 如果失败,尝试匹配一个空格、一个字母和一个点
  • 如果失败,则匹配任何字符
这样,数字/字母后的点已经匹配,不会匹配为正则表达式中紧跟其后的标点符号。

var str=“我离开美国时眼泪汪汪!我知道我会非常想念我的美国朋友。祝你一切顺利”


如果字符串类似于“S.”空格,后跟“单个字符或多位数”,后跟“.”,是否可以防止字符串在语句中中断?事实上,我想了解我们如何才能做到这一点,以便我可以添加更多这样的案例。非常感谢,我正在努力理解这个表达。
result = subject.match(/( \d+\.| [^\W\d_]\.|.)+?([!?.](?= |$)|$)/g);
I left the United States with my eyes full of tears!
 23. I knew I would miss my American friends very much.
 I. All the best to you.
var re =/[^\.\?!]+[\.?!]( +|[^\.\?!]+)/g;
var myArray = str.match(re);
myArray.join('\n')

/*  returned value: (String)
I left the United States with my eyes full of tears! 
I knew I would miss my American friends very much.All the best to you
*/