如何在 JavaScript 中删除字符串中的空格
本文将介绍从字符串中删除空格的不同方法,特别是如何删除制表符和换行符。下面的每个方法都会有一个代码示例,你可以在你的机器上运行。
replace()
方法接受一个指定的值或一个正则表达式,并将它们替换到一个给定的字符串中。它返回一个新的字符串,其中指定的值被替换。
string.replace(/ /g, "")
正则表达式包含一个空格字符(" "
)和全局属性。它将搜索字符串中的每一个空格,然后用第二个参数中给出的空字符串替换它们。
<!DOCTYPE html>
<html>
<head>
<title>
Remove spaces from a string using JavaScript
</title>
</head>
<body>
<h2>
Remove spaces from a string using JavaScript
</h2>
<p>
Original string is:
site/ delft stack .com/
</p>
<p>
New Sentence is:
<span class="outputString"></span>
</p>
<button onclick="removeSpacesFromString()">
Clean Spaces
</button>
<script type="text/javascript">
const removeSpacesFromString = () => {
let text1 =
"site/ delft stack .com/";
let text2 =
text1.replace(/ /g, "");
document.querySelector('.outputString').textContent
= text2;
}
</script>
</body>
</html>
输出:
string.replace(/\s+/g, '')
正则表达式模式 s
指的是任何空格符号:空格、制表符和换行符。
<script type="text/javascript">
const removeSpacesFromString = () => {
let text1 =
"site/ delft stack .com/";
let text2 =
text1.replace(/\s+/g, '');
document.querySelector('.outputString').textContent
= text2;
}
</script>
split()
方法将一个字符串分割成一个数组并返回新的数组。
join()
方法将一个数组中的所有元素连接起来,返回一个新的字符串。
我们将使用 split
方法将字符串分割成一个以空格字符" "
作为定界符的数组,然后使用 join
方法将数组转换为一个字符串。它用这两种方法去掉了空白字符。
<html>
<head>
<title>
Remove spaces from a string using JavaScript
</title>
</head>
<body>
<h2>
Remove spaces from a string using JavaScript
</h2>
<p>
Original string is:
site/ delft stack .com/
</p>
<p>
New Sentence is:
<span class="outputString"></span>
</p>
<button onclick="removeSpacesFromString()">
Clean Spaces
</button>
<script type="text/javascript">
const removeSpacesFromString = () => {
let text1 =
"site/ delft stack .com/";
let text2 =
text1.split(" ").join("");
document.querySelector('.outputString').textContent
= text2;
}
</script>
</body>
</html>
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。