2016-08-19 34 views
0

我需要使用jQuery将每个句子从文本中分离出来。使用jQuery分隔每个句子

比方说,我有这样的文字:

jQuery的基础设计,让你舒适的通过你会被要求使用jQuery解决共同问题的工作。 jQuery的1.x和2.x两个版本都支持Firefox,Chrome,Safari和Opera的“当前-1版本”(意味着当前稳定版本的浏览器及其之前的版本)。

我需要实现这样的结果:

Array (
    [0] = "jQuery Fundamentals is designed to get you comfortable working through common problems you'll be called upon to solve using jQuery." 
    [1] = "Both versions 1.x and 2.x of jQuery support "current-1 versions" (meaning the current stable version of the browser and the version that preceded it) of Firefox, Chrome, Safari, and Opera." 
) 

注意的是,在句子有可能是 “”或其他符号,所以我不确定.split()是否会达到正确的结果。

我喜欢写代码我自己的,所以这将是巨大的,如果答案建议只有方法获得的结果,或者你做这件事的想法。

+1

也许是一个正则表达式...除非它们在数字之后出现,否则会在点上分裂? – nem035

+3

简单的答案:你不能!简单的代码不会知道什么是句子,什么不是。用'.'拆分它,你也可以将它拆分为1.x和2.x。将它拆分成'。 '(*后面的空格),当缺少空格时,您可能无法匹配拆分。所以基本上这是不可能的安全的方式... – eisbehr

+4

首先你必须定义什么是一个句子。如果你已经完成了这项任务,你可以继续:) – Marcus

回答

0

试试这个。!

jQuery(document).ready(function($){ 
      var str = "jQuery Fundamentals is designed to get you comfortable working through common problems you'll be called upon to solve using jQuery. Both versions 1.x and 2.x of jQuery support 'current-1 versions' (meaning the current stable version of the browser and the version that preceded it) of Firefox, Chrome, Safari, and Opera."; 
      var lines = str.split('. '); //dot with space(your case) 
      jQuery.each(lines, function(){ 
       alert(this); 
      }); 
     }); 
+0

这是alredy在评论中指出,这是行不通的!除此之外,你的代码还有一些其他的设计问题,并不是很好的代码... – eisbehr

0

通常我们通过split()方法或正则表达式来完成这种类型的工作。 如果您明确知道文本的类型,就像拥有一组模式,那么您可以使用正则表达式。但有时正则表达式比其他一些基本方法慢。

在你的情况,一个简单的建议:你可以()通过”与分裂去。 ' - >点和一个空格分隔符来获得下一行。此外,搜索第一个字符作为大写字母的下一行。

str.split(". "); //search for a dot along with a space. 
+0

这是alredy在评论中指出,这是行不通的! – eisbehr

相关问题