Javascript: How to remove text from string

There is a generic requirement while working in javascript that is to remove a particular text from a string. This article will illustrate how to remove a specific text from a string if the text is known or from a specific index position.

Table of Contents:-

Javascript remove substring from a string using replace()

Javascript’s replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

Code Example:-

let dummyString= "Javascript is the most popular language"
let dummySubstring = "is the"
let finalString = dummyString.replace(dummySubstring,'')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String: Javascript is the most popular language
Final String: Javascript  most popular language

Javascript remove substring from a string using split() and join()

The split() method in javascript divides the string into substrings forming an array, and then this array is returned.

The join() method in javascript joins the elements of the array back into a string.

Code Example:-

let dummyString= "Javascript is the most popular language"
let dummySubstring = "is the "
let finalString =  dummyString.split(dummySubstring).join('')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String: Javascript is the most popular language
Final String: Javascript most popular language

Javascript remove substring from a string at a specific index position or a given range of indices

Using substring():-

The substring() method in javascript returns a string segment between start and end indexes or to the end of the string.

The below code has a custom method to remove a text from a string if the indices of the particular text are known using substring().

Function Code:-

function removeTextFromString(_string, sIndex, _substringLength) {
    return _string.substr(0, sIndex) + _string.substr(sIndex + _substringLength);
}

Usage:-

let dummyString= "Javascript is the most popular language"
console.log("Original String: "+ dummyString )
console.log("Final String: "+ removeTextFromString(dummyString,10,7))

Output:-

Original String: Javascript is the most popular language
Final String: Javascript most popular language

Using slice():-

Javascript’s slice() method returns a copy of a part of a string as a new string. The slice() function does not modify the original string.

The below code has a custom method to remove a text from a string if the indices of the particular text are known using slice().

Function Code:-

function removeTextFromString(_string, sIndex, _substringLength) {
  return _string.slice(0,sIndex) +  _string.slice(_substringLength+sIndex) 
}

Usage:-

let dummyString= "Javascript is the most popular language"
console.log("Original String: "+ dummyString )
console.log("Final String: "+ removeTextFromString(dummyString,10, 7))

Output:-

Original String: Javascript is the most popular language
Final String: Javascript most popular language

Read More:

We hope this article helped you to remove text from a javascript string. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top