繁体   English   中英

使用jQuery分隔每个句子

[英]Separate each sentence using 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()是否会取得正确的结果。

我更喜欢自己编写代码,因此,如果答案仅提示实现结果的方法或您的想法,那将是很棒的。

尝试这个。!!!

 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);
            });
        });

通常,我们通过split()方法或正则表达式来进行此类工作。 如果您清楚地知道文本的类型,例如具有一组模式,则可以使用正则表达式。 但是有时候正则表达式比其他一些基本方法要慢。

对于您的情况,有一个简单的建议:您可以将split()与'一起使用。 '->点和一个空格分隔符以获取下一行。 此外,搜索以第一个字符为大写字母的下一行。

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

暂无
暂无

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

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