2016-02-13 58 views
2

我想刪除空白和新行,僅在開頭。我想要一些建議,我正在使用下面的代碼。如何刪除空格和新行,僅在textarea開頭

var myTxtArea = document.getElementById('visionText'); 
myTxtArea.value = myTxtArea.value.replace(/^s|^n/g, ''); 
+2

['.replace(/^[\ s \ r \ n] +/g,'')'](https://regex101.com/r/tB4xP3/1) – Tushar

+1

'/^s |^n /'如果第一個字符是's'或'n'則匹配。 –

+0

請使用搜索:https://stackoverflow.com/search?q=%5Bjavascript%5D+trim+start+string。 –

回答

2

alert(" \n\n I am a string \n starting with whitespace and newline"); 
 
alert(" \n\n I am a string with no starting whitespace and newline".replace(/^\s+/, ""));

var reg = /^\s|\n/g; 

var str = " asdfkadsf[xxxxx]bb"; 

var test = str.replace(reg,"") ; 

試試這個,以及

" \n\n I am a string ".replace(/^\s+/,""); 
+2

投給'/^\ s + /' –

+0

@kaushik thanki/^ \ s + /似乎是正確的一個 – user5206903

-2

好吧,你會嘗試使用功能的裝飾,該功能去掉空格和新行從開始到結束

var myTxtArea = document.getElementById('visionText'); myTxtArea.value = myTxtArea.value.trim();

+0

請注意原始問題中的非常微妙的提示:'只在開始時' – Sebas