我一直在研究一個函數,將單詞之間的空格變爲字符串" "
(空格)。字符串操作,用(空格)替換「」空格
例如,"Hello World. Hi there."
將成爲"Hello(space)world.(space)Hi(space)there."
編輯:我試圖建立這樣一組特定的結構化英語的是如下:
- 結果的初始值設置爲空字符串
- 在參數字符串
- 每個索引如果該索引的字符是空格然後
- 追加「(空格)」導致
- 其他
- 該索引處添加字符導致
- 結束時,如果
- 年底爲
- 返回結果
這是我能到目前爲止。:
function showSpaces(aString)
{
var word, letter;
word = aString
for var (count = 0; count < word.length; count = count + 1)
{
letter = word.charAt(count);
if (letter == " ")
{
return("(space)");
}
else
{
return(letter);
}
}
}
每當我測試這個函數調用,什麼都沒有發生:
<INPUT TYPE = "button" NAME = "showSpacesButton" VALUE ="Show spaces in a string as (space)"
ONCLICK = "window.alert(showSpaces('Space: the final frontier'));">
我剛剛從JavaScript開始。任何幫助,將不勝感激。
-Ross。
沒有必要t o在標題中加入焦點話題。 StackOverflow將在頁面的標題前加上最受歡迎的標籤(在本例中爲JavaScript)。 – 2011-09-13 19:37:35
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – 2011-09-13 19:38:01