Two methods to delete substrings: 1. Use replace() to remove the first substring that appears in the string. The syntax is "string object.replace("substring value","")" ;2. Use replaceAll() to remove all substrings, the syntax is "string object.replaceAll("substring value","")".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
How to delete substrings in a string in es6
1. Use the replace() function
replace() method is used to replace some characters in a string with a substring of other characters.
The specified substring can be deleted when the replacement value is a null character.
var str = "Hello world!"; var n=str.replace("Hello",""); console.log(n);
replace() function only replaces the first matching substring:
var str="Visit Microsoft! Visit Microsoft!"; var n=str.replace("Microsoft",""); console.log(n);
2 , use the replaceAll() function
replaceAll() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression. This function will replace all The matched substring.
Similarly, when the replacement value is a null character, the specified substring can be deleted.
var str = "Hello world!"; var n=str.replaceAll("world",""); console.log(n);
The replaceAll() function will replace all matching substrings:
var str="Visit Microsoft! Visit Microsoft!"; var n=str.replaceAll("Microsoft",""); console.log(n);
[Related recommendations: javascript video tutorial、webfrontend】
The above is the detailed content of How to delete substring in es6. For more information, please follow other related articles on the PHP Chinese website!