請幫助我將兩個句子分成一組單詞和字符並單獨顯示。將兩個句子分成單詞和字符並單獨顯示
我的輸入字符串是這樣的:
如果你沒有哭,我給你糖果。然後我會給你 自制蛋糕。
我現在已經做了,直到被如下:
$(function() {
generateWords();
function generateWords() {
$("#splitedWords").html("");
var Input = "If you didn't cry, I will give you sweets. Then I will give you home-made cakes.";
var outputArray = Input.split(/\s+/); // for spliting each word
// for printing the sorted words inside a button
for (var i = 0; i < outputArray.length; i++) {
$("#splitedWords").append($('<span>' + outputArray[i] + '</span><br/>'));
}
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<h4>Input</h4>
<p>If you didn't cry, I will give you sweets. Then I will give you home-made cakes.</p>
<h4>O/P required <small>(Want to split words, dot, comma, hyphen, semicolon etc into an array)</small></h4>
<p>
var outputArray = [ "If", "you", "didn't", "cry", ",", "I", "will", "give", "you", "sweets", ".", "Then", "I", "will", "give", "you", "home-made", "cakes", "." ];
</p>
<h4>Actual O/P</h4>
<p id="splitedWords"></p>
</body>
</html>
是我的方法不對?任何幫助表示讚賞:)
僅供參考jQuery jQuery是一個用於修改DOM的庫,在這種情況下它沒有用。 –
JavaScript具有分割功能。在這種情況下,它不會得到你想要的,但這是一個好的開始。 'var str =「你的字符串」; str.split(「」);'..看看這是否適用於你 – Krishna
@depperm我會發布我迄今爲止所做的,我已經使用JavaScript分裂方法和reg-ex。 :) –