在 JavaScript 中从字符串中删除字符
JavaScript 有不同的方法来删除字符串中的特定字符。我们将介绍如何在 JavaScript 中删除字符串中的字符。
在 JavaScript 中,我们使用正则表达式的 replace()
方法来删除字符串中指定字符的所有实例。
replace(/regExp/g, '');
<!DOCTYPE html>
<html>
<head>
<title>
How to remove all instances of the specified character in a string?
</title>
</head>
<body>
<h1>
DelftStack
</h1>
<b>
How to remove all instances of the specified character in a string?
</b>
<p>The original string is DelftStack</p>
<p>
New Output is:
<span id="outputWord"></span>
</p>
<button onclick="removeCharacterFromString()">
Remove Character
</button>
<script type="text/javascript">
const removeCharacterFromString = () => {
originalWord = 'DelftStack';
newWord = originalWord.replace(/t/g, '');
document.querySelector('#outputWord').textContent
= newWord;
}
</script>
</body>
</html>
输出:
The original string is DelftStack
New Output is: DelfSack
当我们需要删除一个字符时,当一个字符串中有多个该字符的实例时,例如,从字符串 DelftStack
中删除字符 t
,我们可以使用 slice()
方法得到给定索引前后的两个字符串,并将其连接起来。
<!DOCTYPE html>
<html>
<head>
<title>
How to remove Specified Character at a Given Index in a string?
</title>
</head>
<body>
<h1>
DelftStack
</h1>
<b>
How to remove Specified Character at a Given Index in a string?
</b>
<p>The original string is DelftStack</p>
<p>
New Output is:
<span id="outputWord"></span>
</p>
<button onclick="removeCharacterFromString(5)">
Remove Character
</button>
<script type="text/javascript">
const removeCharacterFromString = (position) => {
originalWord = 'DelftStack';
newWord = originalWord.slice(0, position - 1)
+ originalWord.slice(position, originalWord.length);
document.querySelector('#outputWord').textContent
= newWord;
}
</script>
</body>
</html>
我们可以使用 replace()
方法,但不使用正则表达式,在 JavaScript 中只删除字符串中的第一个字符实例。我们把要删除的字符作为第一个参数,把空字符串''
作为第二个参数。
<!DOCTYPE html>
<html>
<head>
<title>
How to remove First Instance of Character in a string?
</title>
</head>
<body>
<h1>
DelftStack
</h1>
<b>
How to remove First Instance of Character in a string?
</b>
<p>The original string is DelftStack</p>
<p>
New Output is:
<span id="outputWord"></span>
</p>
<button onclick="removeCharacterFromString()">
Remove Character
</button>
<script type="text/javascript">
const removeCharacterFromString = () => {
originalWord = 'DelftStack';
newWord = originalWord.replace('t', '');
document.querySelector('#outputWord').textContent
= newWord;
}
</script>
</body>
</html>
相关文章
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
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。