2017-01-23 24 views
-2
input = ["hello \"shahrukh\" ", " hello \"dude \" "] ; 

output = ' [hello \"shahrukh\", hello \"dude \" ]' ; 

基本上,我需要去除所有的雙引號的output如果他們在開頭或input array元素的結束。如果outputstring,那很好。正則表達式代用品

我試過使用正則表達式替代品但沒有運氣。任何幫助表示讚賞。

+0

沒有道理。如果你解釋爲什麼你想要它,那麼很確定你正在做錯誤的事情,這可以正確完成。 – dfsq

+0

如果我很好理解,你想從數組中構建一個字符串?但爲什麼?你想做什麼?我很確定這是一個XY問題:http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Toto

回答

0

有一種方法稱爲JSON.stringify()從對象或數組中創建有效的JSON字符串。由此產生的字符串包含",你可能不喜歡。我認爲最簡單的方法來生成無效 JSON字符串沒有"是使用數組方法.join()

// using JSON.stringify() 
 
var input = ["hello \"shahrukh\" ", " hello \"dude \" "]; 
 
console.log(JSON.stringify(input));

// using array.join() 
 
var input = ["hello \"shahrukh\" ", " hello \"dude \" "]; 
 
console.log("["+input.join()+"]");