2013-08-17 52 views
0

好吧,假設您有一個字符串:"(hello) (world) (something) (number 4)" 而在JavaScript中,您希望按照時間順序獲取括號內的內容。你可以使用indexOf(),但你怎麼處理多種可能性。處理多個可能的indexOf()

+2

檢查:[使用Javascript正則表達式 - 如何讓捲髮之間的文本括號](http://stackoverflow.com/questions/3354224/javascript-regex-how-to-get-text-between-curly-brackets) – Zbigniew

+1

你是什麼意思與「按時間順序」的順序? –

+0

from num 1 to last – Pixeladed

回答

1

使用matchmap可能是一個想法:

"(hello) (world) (something) (number 4)" 
.match(/\(.+?\)/g) 
.map(function(a){return a.replace(/[\(\)]/g,'');}) 
//=> ["hello", "world", "something", "number 4"] 

MDNArray.prototype.map方法,還提供了一個墊片對舊版瀏覽器

+0

that worked,but I do not get the'.map' methods – Pixeladed

+0

did'map' method does replace all'()'nothing nothing?你是怎麼把它放在一個數組中的? – Pixeladed

+0

'匹配方法'結果是一個包含字符串的所有帶括號的單詞的數組(按照正則表達式'/\(.+?\)/ g')。 'map'方法用一個空字符串替換該數組中每個元素的括號。 – KooiInc