正则表达式结果跳过多个空格

Regex result skips multiple spaces

这是我的问题的一个最小示例:

http://jsfiddle.net/pm913emb/5/

var string = 'Question 6 of 7 '
+'Three, the patient suddenly develops shortness of breath and becomes hypotensive.    His heart rate is 100/min, with a normaI PR and    QRS intervaI.'

var sentencesMatch = string.match(/([\sa-zA-Z\d]){1}.+?[\.!\?]{1}([\s ]+|$)/g);

console.log(sentencesMatch);

如您所见,这个字符串包含多个句子,我在两个地方添加了多个空格:一个在句子的末尾,另一个在句子的中间。有正则表达式,我在这个字符串上 运行 。

问题是:正如您在控制台中看到的,匹配的结果不包含这些多个空格。

这个问题可能是什么原因造成的。和可能的解决方案?

请帮忙.. :/

这不是您的正则表达式或您拥有的字符串的问题,如果您尝试输入 '\n'。你会看到它基本上只是用一个 space 替换它,因此问题出在你的浏览器上。你可能想像这样添加 header 来修复它:

content-type: text/html

或在需要时尝试对其进行 base64 编码。解码它。

浏览器不会显示连续的白色-space。如果您要使用实体,它们 space 将被显示。例如

</code> <-- 2 spaces </p> <p> 将显示为 </p> <p><code> <-- 一个 space

在浏览器中。

如果您为 spaces

使用了实体

&#160;&#160;

你会得到

</code> (2 white-spaces (注意即使这里是一个 spaced).</p> <p>这里有一篇更长的文章。</p> <p><a href="">Browser white space rendering</a></p> <p>我认为这可以实现你想要的(可能不是最干净的,我不经常写 JS)..</p> <pre><code><script type="text/javascript"> var string = 'Question 6 of 7 ' +'Three, the patient suddenly develops shortness of breath and becomes hypotensive. His heart rate is 100/min, with a normaI PR and QRS intervaI.' var sentencesMatch = string.match(/([\sa-zA-Z\d]){1}.+?[\.!\?]{1}([\s ]+|$)/g); var output = ''; for(var x= 0; x < sentencesMatch.length; x++){ output += sentencesMatch[x].replace(/ /g, '&#160;'); } document.write(output); </script>

您的代码有效

就在您尝试打印数组本身时,浏览器会在控制台中修剪多余的白色 space。尝试打印单个数组元素,(取决于您的浏览器)您会看到它们确实包含额外的 spaces.

//You'll need to have the console open to see the results here

var string = 'Question 6 of 7 '
+'Three, the patient suddenly develops shortness of breath and becomes hypotensive.    His heart rate is 100/min, with a normaI PR and    QRS intervaI.'

var sentencesMatch = string.match(/([\sa-zA-Z\d]){1}.+?[\.!\?]{1}([\s ]+|$)/g);
console.log(sentencesMatch);

for (var i in sentencesMatch){
    //Add quotes so we can see trailing whitespace
    console.log('"' + sentencesMatch[i] + '"'); 
}

在 HTML

中默认修剪额外的白色 space

如果您想将该字符串实际放入一个元素中,那么您将遇到同样的问题。修复方法如下:

使用CSS

可能是最简单的解决方案。使用 white-space 属性

设置元素样式

var string = 'Question 6 of 7 '
+'Three, the patient suddenly develops shortness of breath and becomes hypotensive.    His heart rate is 100/min, with a normaI PR and    QRS intervaI.'

var sentencesMatch = string.match(/([\sa-zA-Z\d]){1}.+?[\.!\?]{1}([\s ]+|$)/g);
for (var i in sentencesMatch){
  var p = document.createElement("p");
  document.body.appendChild(p);
  p.innerHTML = '"' + sentencesMatch[i] + '"';
  p.className = "keep-spaces";  
}
.keep-spaces{
  white-space: pre;
}

或者..用不间断的-space

替换白色space

此解决方案将所有白色space 字符替换为 'non-breaking-space'。这由 HTML 实体 &nbsp;&#160;&xa0;.

表示

var string = 'Question 6 of 7 '
    +'Three, the patient suddenly develops shortness of breath and becomes hypotensive.    His heart rate is 100/min, with a normaI PR and    QRS intervaI.'
var sentencesMatch = string.match(/([\sa-zA-Z\d]){1}.+?[\.!\?]{1}([\s ]+|$)/g);

for (var i in sentencesMatch){
  var p = document.createElement("p");
  document.body.appendChild(p);
  //Replace spaces with &nbsp; to preserve consecutive white space
  var str = sentencesMatch[i].replace(/\s/g,'&nbsp;');
  p.innerHTML = '"' + str + '"';
}