[英]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.