使用jQuery分隔每个句子

Separate each sentence using jQuery

本文关键字:句子 分隔 jQuery 使用      更新时间:2023-09-26

我需要使用jQuery将每个句子从文本中分离到数组项中。

假设我有这样的文本:

jQuery基本原理旨在让您轻松地解决您将使用jQuery解决的常见问题。两个版本1。X和2。jQuery的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() by '。' ->点和空格分隔符以获得下一行。进一步,搜索第一个字符为大写的下一行。

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