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

Detailed explanation of the murder caused by a / slash in Nginx proxy_pass

background An nginx server module needs to proxy ...

CSS3 Tab animation example background switching dynamic effect

CSS 3 animation example - dynamic effect of Tab b...

Steps to package and release the Vue project

Table of contents 1. Transition from development ...

Introduction to the B-Tree Insertion Process

In the previous article https://www.jb51.net/arti...

HTML tags explained

HTML tags explained 1. HTML tags Tag: !DOCTYPE De...

Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?

I have been using CSS for a long time, but I have...

An article to help you understand jQuery animation

Table of contents 1. Control the display and hidi...

Sample code for implementing radar chart with vue+antv

1. Download Dependency npm install @antv/data-set...

Vue implements online preview of PDF files (using pdf.js/iframe/embed)

Preface I am currently working on a high-quality ...

ul list tag design web page multi-column layout

I suddenly thought of this method when I was writi...

In-depth explanation of the principle of MySQL Innodb index

introduction Looking back four years ago, when I ...

Example code for implementing dynamic skinning with vue+element

Sometimes the theme of a project cannot satisfy e...