JavaScript 正規表現とreplaceメソッドを使って改行を全て削除する

環境
Google Chrome 111.0.5563.147
Windows 10 Home 64bit

構文
文字列.replace(/\r?\n/g, ");
「replace」を使用すると、空文字に置換に置換して、改行を削除します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let strA = `study
skill
arkgame`
console.log(strA)
console.log(strA.replace(/\r?\n/g, ''))
let strA = `study skill arkgame` console.log(strA) console.log(strA.replace(/\r?\n/g, ''))
let strA = `study
skill
arkgame`

console.log(strA)

console.log(strA.replace(/\r?\n/g, ''))

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> "study
skill
arkgame"
> "studyskillarkgame"
> "study skill arkgame" > "studyskillarkgame"
> "study
skill
arkgame"
> "studyskillarkgame"

 

JavaScript

Posted by arkgame