2016-01-31 31 views
0

我有一個這樣的字符串:如何刪除字符串中的一系列索引位置?

var str = "T h i s  i s  a    t e s t"; 
//   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 

現在我想刪除此範圍:[11 - 13],我想這樣的輸出:

var newstr = "T h i s  i s  a  t e s t"; 
//   1 2 3 4 5 6 7 8 9 10 14 15 16 17 

我怎麼能這樣做?

+2

要與一個替換乘空格? 'str.replace(/ \ s +/g,'');' – andlrc

+1

這可能是以下的副本:http://stackoverflow.com/questions/1981349/regex-to-replace-multiple-spaces-with-a-單空間 – Joran

+1

@ dev-null解決方案更好地移除多個空格,但是我想更正它。用一個空格替換一個或多個空間。 '.replace(/ \ s +/g,'')'//注意,第二個參數,一個空格 – Tushar

回答

2

這應做到:

var newstr = str.slice(0, 10) + str.slice(14); 
相關問題