Remove substring from string javascript – How to Remove a Substring from a String using JavaScript

Remove substring from string javascript: To remove a substring from a string, use the replace() function with the substring and an empty string as inputs, for example, gvn_str.replace (“example”, “”).

The replace() function returns a new string that removes the first occurrence of the specified substring.

Let us now see some of the below examples to understand this concept more clearly.

Removing a Substring From a String using JavaScript

1)Removing the first occurrence of ‘hello’ from a given string

// Give the string as static input and store it in a variable.
const gvn_str = 'hello,hello,Btechgeeks';

// Removing the first occurrence of 'hello' from a given string 
// using the replace() function
const remove_firststoccur = gvn_str.replace('hello', '');
console.log(remove_firststoccur); 

Output:

,hello,Btechgeeks

2)Removing All occurrences of ‘hello’ from a given string

// Give the string as static input and store it in a variable.
const gvn_str = 'hello,hello,Btechgeeks';

// Removing all occurrences of 'hello' from a given string
// using the replaceAll() function
const remove_all_occur = gvn_str.replaceAll('hello', '');
console.log(remove_all_occur);

Output:

,,Btechgeeks

3) Removing the first occurrence of hello from a given string using regex

// Give the string as static input and store it in a variable.
const gvn_str = 'hello,hello,Btechgeeks';

// Removing first occurrence of hello from a given string 
// using regex and replace() function
const removing_usingRegex = gvn_str.replace(/hello/, '');
console.log(removing_usingRegex);

Output:

,hello,Btechgeeks

4)Removing all occurrences of ‘hello’ from a given string using regex

// Give the string as static input and store it in a variable.
const gvn_str = 'hello,hello,Btechgeeks';

// Removing all occurrences of 'hello' from a given string 
// using regex and replace() function
const removeAllOccur_regex = gvn_str.replace(/hello/g, '');
// Print it on the console
console.log(removeAllOccur_regex);

Output:

,,Btechgeeks

string.replace() function:

Remove substring javascript: The String.replace() function accepts the following two parameters:

  • A substring of the string that we wish to match.
  • Replacement for the first match.

Here we provided an empty string as the replacement since we want to remove the substring.

NOTE:

The replace method returns a new string rather than changing the old string. 
Strings in JavaScript are immutable.

string.replaceAll() function:

Javascript remove substring from string: Use the string.replaceAll() function to remove all occurrences of a substring from a string.

To delete all occurrences of a substring from a string, use the replaceAll() method with the substring as the first parameter and an empty string as the second.

The replaceAll method will return a new string that removes all occurrences of the substring.

The replaceAll() function takes the same 2 arguments as the replace() method takes

  • The search string
  • The replacement for each match.

Example

JS remove substring from string: The forward slashes / / indicate the start and end of the regular expression. Within the regular expression, we have a character class [] that matches all digits from 0 to 9. The first example replaces the first occurrence of a digit in the string with an empty string.

Approach:

  • Give the string as static input and store it in a variable.
  • Remove the first occurrence of any number between 0 to 9(included) from a given string using regex and replace it with an empty string using replace() function
  • Print the result on the console.
  • Remove All the occurrences of any number between 0 to 9(included) from a given string using regex and replace it with an empty string using replace() function.
  • The Exit of the Program.

Below is the implementation:

// Give the string as static input and store it in a variable.
const gvn_str = '1, one, 5, 9';

// Remove first occurrence of any number between 0 to 9(included)
// from a given string using regex and replace it with an empty
// string using replace() function
const removeFstOccur_regex  = gvn_str.replace(/[0-9]/, '');
// Print the result on console
console.log(removeFstOccur_regex); 

// Remove All the occurrences of any number between 0 to 9(included)
// from a given string using regex and replace it with an empty
// string using replace() function
const removeAllOccur_regex  = gvn_str.replace(/[0-9]/g, '');
console.log(removeAllOccur_regex ); 

Output:

, one, 5, 9
, one, ,

Explanation:

Here in the first case, 1 is a number between 0 to 9(both included) hence it is replaced with 
an empty string. Removing only the first occurence keep the rest numbers undisturbed.

In the second case, 1, 5, 9 are the numbers in a range 0 to 9, hence all of them are replaced 
with an empty string. Removing all the occurences

NOTE:

We can also replace it with any other string instead of an empty string

Example

// Give the string as static input and store it in a variable.
const gvn_str = '1, one, 5, 9';

// Remove first occurrence of any number between 0 to 9(included)
// from a given string using regex and replace it with "@" symbol
// using replace() function
const removeFstOccur_regex  = gvn_str.replace(/[0-9]/, '@');
// Print the result on console
console.log(removeFstOccur_regex); 

// Remove All the occurrences of any number between 0 to 9(included)
// from a given string using regex and replace it with "@" symbol
// using replace() function
const removeAllOccur_regex  = gvn_str.replace(/[0-9]/g, '@');
console.log(removeAllOccur_regex ); 

Output:

@, one, 5, 9
@, one, @, @