SoFunction
Updated on 2025-03-10

JavaScript: Methods for deleting or extracting strings specified characters (extremely common)

()

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

let  a = "1,2,3";
((2,))
//The first parameter starts with a character with a subscript of 0, including the current subscript of 0,//The second parameter ends with the character with the subscript of the subscript, and does not include the current subscript. By default, the subscript position length of the last character of the substring to be extracted is +1 more than the subscript position in the string. 
//The total length is +1 from the second subscript, so the result output is: 2,3

()

Methods can extract the specified number of characters from the subscript to the subscript of several characters in the string.

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

()

The method can return the first location of a specified string value in the string. If no match is found, it will return -1.

var str="runab site";
var n=("a");
//If you only specify one parameter, you will query it from scratch, specify the string value to be retrieved, and return the specified subscript after querying the value.//The second parameter specifies the location where the search starts in the string (including the input subscript). When querying, return the specified subscript.//The output result is: 3

()

The method can return the last location of the specified string value. If no matching string is found, the return -1 will be returned.

var str="runab site";
var n=("a",3);
// Only specify one parameter will be searched from the tail to return the specified subscript.//The second parameter is to query forward from the characters with the subscript of several (including the input subscript), and return the specified subscript to the query.//The output result is: 3 
 
//Use it in conjunction with the above (return to the last character)let str= (0, ('e'));

()

Methods are used to replace other characters in a string with some characters, or to replace a substring that matches a regular expression.

var str="Hello everyone!";
var n=("big","Small");
//The output result is: Good family! 
 
//You can also replace it with regularvar str="Everyone is so big";
var n=(/big/g,"Small");  //g is a global replacement//The output result is: The small family is so small 
//Set the second parameter to empty to delete the stringvar str="Everyone is so big";
var n=(/big/g,"");  //g is a global replacement//The output result is: Good home 

Sometimes characters need to be spliced ​​after intercepting, so

var str="hello world!"
var items=("ll")      // ["he", "oWorld!"]
//An array will be obtained, and the items array includes multiple strings divided by ll (excluding ll)var newStr=("");       // heoWorld!
//join() uses an empty string to connect the array into a new string without quotes, and the default comma-separation is not added.

Replenish:

  • slice(start, end) The method extracts a part of the string and returns the extracted part as a new string. usestart(Included) andend(Not included) Parameters to specify the part of the string extraction, and passing a negative number is to intercept from the later.
  • includes() Method is used to determine whether a string contains the specified substring, and if a matching string is found, it returnstrue, otherwise returnfalse
  • search() Methods are used to retrieve substrings specified in strings, or to retrieve substrings that match regular expressions.
  • match() Methods can retrieve the specified value within a string, or find a match for one or more regular expressions.
  • test() Method is used to retrieve the specified value in a string. Returns true or false.
  • exec() Methods are used to retrieve matches to regular expressions in strings. Returns an array where matching results are stored. If no match is found, the return value is null.

Here's the article aboutJavaScript This is the article about deleting or extracting specified characters in strings (extremely common). For more related JavaScript methods for deleting or extracting specified characters in strings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!