2014-03-28 61 views
-1

這是我的代碼:如何刪除字符串中的換行符?

process.stdin.resume(); 
process.stdin.setEncoding('utf8'); 

process.stdin.on('data', function (text) { 
    text.replace(/\r?\n|\r/g, ""); 
    console.log("Command: " + text + "textthatshouldbeinthesameline"); 
    if (text == 'quit') { 
     console.log("Quitting"); 
     done(); 
    } 
}); 

function done() { 
    console.log('Now that process.stdin is paused, there is nothing more to do.'); 
    process.exit(); 
} 

這是發生了什麼:

enter image description here

好像有一些換行,我試圖取代他們,但你看:無影響。

當我輸入「quit」時,它也不會被if語句識別。

回答

6

replace不會更改字符串,因爲字符串是不可變的。它返回一個新的字符串。

變化

text.replace(/\r?\n|\r/g, ""); 

text = text.replace(/\r?\n|\r/g, ""); 
+0

事實上O.o愚蠢的錯誤,我雖然是更復雜的東西:P – Piotrek