2013-07-21 41 views
-3

這是一個嘰嘰喳喳,我需要轉換字符串數組,該怎麼辦?做,因爲我需要通過單獨的每個元素進行迭代...如何轉換字符串數組?

var twitter = 'RT Informacion sobre algo en comun @opmeitle #demodelor union J, http://bit.ly/a12, [email protected]'; 

我需要類似的東西。

var result = ['RT, Informacion, sobre, algo, en, comun, @opmeitle, #demodelor, union, J, http://bit.ly/a12, [email protected]'] 

for (i in result) { console.log(result[i]) } // output >> 
RT 
Informacion 
sobre 
... 

使用JavaScript或的NodeJS

+0

你想要什麼? – null

+0

將twitt轉換爲數組。 – opmeitle

+0

你想分解成單詞嗎? – wwww

回答

3

像你需要這樣的事情在我看來:

var string = "hi coldfusion stackoverflow"; 
var array = string.split(' ') 

此代碼分割字符串到一個數組通過傳入的參數爲.split這這種情況是一個空間," "。當執行.split時,所有空格(因爲我們傳入一個空格)被刪除,並在空格之間創建一個數組的新元素(?)。

2
// This splits result into words -- /\s+/ is a regex 
// that detects one or more whitespace characters 
var twitt = 'foo bar baz quux'; 

var result = twitt.split(/\s+/); 
// result is now ['foo', 'bar', 'baz', 'quux'] 

for (var i = 0; i < result.length; i++) { 
    console.log(result[i]); 
} 

避免使用for in循環遍歷數組。

+0

這個真正的問題! var twitter ='RT Informacion sobre algo en comun @opmeitle #demodelor union J,http://bit.ly/a12,[email protected]'; – opmeitle

+0

好的,謝謝!容易,對這篇文章感到抱歉! :d – opmeitle

相關問題