Home > Web Front-end > JS Tutorial > body text

How to delete specified characters in javascript string

青灯夜游
Release: 2023-01-05 16:11:36
Original
47684 people have browsed it

Deletion method: 1. Use the "str.replace(regular expression, '')" statement; 2. Use slice(), the syntax "str.splice(character position, number of deletions);"; 3. , use substring(), the syntax is "str.substring(start subscript, end subscript)".

How to delete specified characters in javascript string

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method 1: Use the replace() method

The replace() method is used to replace specific characters/strings with other characters/strings. It takes two parameters, the first is the string to replace and the second is the string to replace.

In this case, the first argument is the character to be removed, and the second argument can be given as an empty string; this removes the character from the string. But this method removes the first occurrence of the string.

To delete all occurrences of a specified character, you can use the replace() method with a regular expression. Use regular expressions instead of strings and global properties. It will select every occurrence in the string and remove it.

Example:

How to delete specified characters in javascript string

Method 2: Using slice() method

slice() method is used for extraction The portion of the string between the given arguments. This method gets the starting and ending index of a string and returns the string between these indices. If the ending index is not specified, it is assumed to be the length of the string.

The first character can be removed by specifying the starting index as 1. It extracts the string from the second character to the end of the string. The last character can be removed by specifying the ending index as one less than the length of the string. This will extract the string from the beginning to the second to last character.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>原始字符串是:hello hgbhggfj!</p>
<p>删除字符串的第一个字符:
<span class="output1"></span>
</p>
<p>删除字符串的最后一个字符:
<span class="output2"></span>
</p>
<script type="text/javascript">
originalString = &#39;hello hgbhggfj!&#39;;
firstCharRemoved = originalString.slice(1);
lastCharRemoved =
originalString.slice(0, originalString.length - 1);
document.querySelector(&#39;.output1&#39;).textContent = firstCharRemoved;
document.querySelector(&#39;.output2&#39;).textContent = lastCharRemoved;
</script>
</body>
</html>
Copy after login

Output:

原始字符串是:hello hgbhggfj!
删除字符串的第一个字符: ello hgbhggfj!
删除字符串的最后一个字符: hello hgbhggfj
Copy after login

Method 3: Use the substring() method

The substring() method is used to extract the string between two specified characters between subscripts.

The substring() method returns a substring that includes the characters at the beginning, but does not include the characters at the end.

Example:

var str="aabbccdd";
console.info(str.substring(4));  //得到ccdd
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to delete specified characters in javascript string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template