使用空格替代换行符
关键语句:
string.replace(/\u000A/g, ' ') // 使用空格替代换行符
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用空格替代换行符</title>
<style type="text/css">
* {
font-family: "courier new";
padding: 0;
margin: 0;
box-sizing: border-box;
}
div {
width: 100%;
height: 50vh;
padding: 5px;
}
</style>
</head>
<body>
<div>
<textarea id="input" autofocus></textarea>
</div>
<div style="padding: 0 5px 5px;">
<textarea id="output"></textarea>
</div>
</body>
<script>
window.onload = (event) => {
const textareaInput = document.getElementById('input')
const textareaOutput = document.getElementById('output')
textareaInput.style = "width: 100%; height: 100%; padding: 5px;"
textareaInput.placeholder = "input"
textareaOutput.style = "width: 100%; height: 100%; padding: 5px;"
textareaOutput.placeholder = "output"
const regexp = /\u000A/g; // 换行符的 Unicode 编码
const replacement = ' '; // 空格
textareaInput.addEventListener('input', function(event) {
textareaOutput.value = this.value.replace(regexp, replacement); // 使用空格替代换行符
})
textareaOutput.addEventListener('click', function(event) {
// 选中所有字符
this.selectionStart = 0
this.selectionEnd = this.value.length
document.execCommand('copy') // 复制选中的内容
})
}
</script>
</html>