JavaScript method to delete or extract specified characters from a string (very commonly used)

JavaScript method to delete or extract specified characters from a string (very commonly used)

1. substring()

Method is used to extract characters between two specified subscripts in a (string).

let a = "1,2,3";
document.write(a.substring(2,a.Length))
//The first parameter starts from the character with subscript 0, including the current subscript 0,
//The second parameter ends at the character with the subscript, excluding the current subscript. By default, it is +1 longer than the subscript position of the last character of the substring to be extracted in the string
 
//Truncate from the second subscript to the total length + 1, so the result output is: 2,3

2. substr()

The method can extract the characters from the subscript to the specified number of characters in the string.

var str="Hello!";
var n=str.substr(2,3)
// Extract 3 characters from the character with subscript 2, and the output result is: llo

3.indexOf()

The method returns the position of the first occurrence of a specified string value in a string, or -1 if no match is found.

var str="runab site";
var n=str.indexOf("a");
//If only one parameter is specified, the query will start from the beginning, specify the string value to be searched, and return the specified index after the query //The second parameter specifies the position to start searching in the string (including the input index), and returns the specified index after the query //The output result is: 3

4.lastIndexOf()

The method returns the position of the last occurrence of a specified string value, or -1 if no matching string is found.

var str="runab site";
var n=str.lastIndexOf("a",3);
//If only one parameter is specified, the query will start from the end and go forward. The specified index will be returned after the query. //The second parameter is the character with the subscript number to query forward (including the input subscript). The specified index will be returned after the query. //The output result is: 3
 
 
//Combined with the above (return the last character)
let str = str.substring(0, str.lastIndexOf('e'));

5.replace()

The method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.

var str="Hello everyone!";
var n=str.replace("big","small");
//The output result is: Hello, little family!
 
 
//You can also use regular expression to replace var str="everyone is so big";
var n=str.replace(/大/g,"小"); //g is global replacement //output result is: 小家好小 //set the second parameter to empty to delete the string var str="大家好大";
var n=str.replace(/大/g,""); //g is a global replacement //The output result is: 家好

Sometimes the characters need to be spliced ​​after being intercepted, which can be done like this

var str="hello world!"
var items = str.split("ll") // ["he", "oWorld!"]
//You will get an array, the items array includes multiple strings separated by ll (excluding ll)
var newStr=items.join(""); // heoWorld!
//join() uses an empty string to connect the arrays in the array into a new string. Without quotes, commas are used as separators by default.

Replenish:

  • slice(start, end) method extracts a portion of a string and returns the extracted portion as a new string. Use start (inclusive) and end (exclusive) parameters to specify the part of the string to extract. Passing a negative number means extracting from the end.
  • The includes() method is used to determine whether a string contains a specified substring. If a matching string is found, it returns true , otherwise it returns false .
  • The search() method is used to search for a specified substring in a string, or to search for a substring that matches a regular expression.
  • match() method searches for a specified value within a string, or finds matches for one or more regular expressions.
  • test() method is used to retrieve the value specified in a string. Returns true or false.
  • exec() method is used to retrieve matches of a regular expression within a string. Returns an array containing the matching results. If no match is found, the return value is null.

This concludes this article about how to use JavaScript to delete or extract specified characters from a string (extremely commonly used). For more information about how to use JavaScript to delete or extract specified characters from a string, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript data type conversion example (converting other types to strings, numeric types, and Boolean types)
  • Four practical tips for JavaScript string operations

<<:  Completely delete MySQL steps

>>:  Detailed explanation of homology and cross-domain required for front-end interviews

Recommend

How to create a file system in a Linux partition or logical volume

Preface Learn to create a file system on your sys...

25 Vue Tips You Must Know

Table of contents 1. Limit props to type lists 2....

Learn about JavaScript closure functions in one article

Table of contents Variable Scope The concept of c...

How to install Graphviz and get started tutorial under Windows

Download and installConfigure environment variabl...

MySQL master-slave replication principle and practice detailed explanation

Table of contents Introduction effect principle f...

Detailed example of sorting function field() in MySQL

Preface In our daily development process, sorting...

Solution to incomplete text display in el-tree

Table of contents Method 1: The simplest way to s...

CSS Transition expands and collapses elements by changing the Height

A common development need is that we want to coll...

Understanding and using React useEffect

Table of contents Avoid repetitive rendering loop...

Meta tags in simple terms

The META tag, commonly referred to as the tag, is...

Detailed explanation of the syntax and process of executing MySQL transactions

Abstract: MySQL provides a variety of storage eng...

How to package the uniapp project as a desktop application

Installing Electron cnpm install electron -g Inst...

VMware Workstation 14 Pro installs CentOS 7.0

The specific method of installing CentOS 7.0 on V...

How to solve the high concurrency problem in MySQL database

Preface We all know that startups initially use m...