How to Remove a Text from a String in JavaScript? – Definitive Guide

One of the main requirements programmers face while working with JavaScript is to remove a particular text from the string.

You can remove a text from a String in JavaScript using string.substring(startvalue, endvalue) statement.

There are other ways to accomplish this task.

If You’re in Hurry…

You can use the below code snippet to remove a text from a string in JavaScript.

string.substring(startvalue, endvalue);

If You Want to Understand Details, Read on…

In this tutorial, you will learn about the various methods to remove text from a JavaScript string and how to remove specific characters from a string.

When you are finished, you will understand the various methods in the context of removing a text from a string.

Using Substr() method

substr()is a built-in function in JavaScript that is used to extract the portion of text from the given string.

The string’s start index and length are passed as parameters.

You can use this method when the text indices to be extracted are unknown.

The syntax to obtain the text from the given string using the substr() method is as follows

Syntax

string.substr(startindex, string length)

start_index – the starting position of the string to be removed
string length – the length of the string to be removed

The following code extracts the text starting at position 5 for ten characters long.

Code

let str = 'this is the original string';
let str1 = str.substr(5,10);
console.log(str1);

Output

is the ori

Using substring() method

The substringmethod extracts characters from the start index to the end index (excluded).

The start index and end index are passed as parameters.

When to use: This method is used when the text indices to be extracted are known.

The parameters are swapped if the start value specified is greater than the end index.

Any negative indices are treated as zero.

The syntax to use the substring() method follows.

Syntax

string.substring(start_index,end_index)

start_index – the starting position of the string to be removed

end_index – the final position of the string to be removed

Code

let str = 'this is the original string';
let str1 = str.substring(5,10);
console.log(str1);

Output

is th

Using slice() method

The slice()method returns a new string containing the extracted portion of the original string

It accepts two parameters. start index and end index. These indices indicate the part of the string to be extracted.

Passing a negative number as a parameter selects from the end of the string.

Syntax

string.slice(start_index, end_index)

start_index – the starting position. Mandatory
end_index – the end index. Optional

The following code extracts the text from the given string based on the start and end value.

Code

let str = 'This is the original string';
let str1=str.slice(0,3);
let str2=str.slice(0,-7);
let str3=str.slice(12);
let str4=str.slice(29);
console.log('The extracted text is ' + str1);
console.log('The extracted text is ' + str2);
console.log('The extracted text is ' + str3);
console.log('The extracted text is ' + str4);

Output

The extracted text is Thi 
The extracted text is This is the original
The extracted text is original string
The extracted text is 

Using replace() method

The replace() method removes a specific text from the given string by replacing it with blank space.

The syntax to remove a text from the given string using the replace() method is shown below.

Syntax

string.replace('text to remove', '')

The following code removes the word ‘text’ from the string.

Code

let str = 'Remove text from string';
let newstr = str.replace('text','');
console.log(newstr);

Output

Remove from string

Using replace() method with regex

The replace() method is used to remove all the occurrences of a specific text from the given string by replacing it with blank space.

The syntax to remove a text from the given string using the replace() method with regex is as follows.

Syntax

string.replace(/text to remove/g, '')

g – global flag to be used with regex

The following code removes the word ‘text’ from the string.

Code

let str = 'she sells shells, She sells pearls';
let newstr = str.replace(/she/g,'');
console.log(newstr);

Output

sells shells, sells pearls

Using split and join method

The split()method splits the given string into substrings where each substring can be treated as an array.

The join() method, on the other hand, joins the elements of the array to form a new string.

Syntax

string.split('text to be removed').join('')

The following code shows how to remove a specific text from the string using the split and join method.

Code

let originalstring = 'how to remove text from a string'; 

let substr = 'how to'; 

let splitstring = originalstring.split(substr); 

let newstr = splitstring.join('');

console.log("Original String: " + originalstring); 

console.log("Split String: " + splitstring); 

console.log("Final string: " + newstr);

Output

Original String: how to remove text from a string
Split String: , remove text from a string
Final string:  remove text from a string

Remove a Specific Character from String

Both replace() and replaceAll() will remove a specific character from the string in JavaScript.

replace() method removes the first occurrence of the character from the string, whereas replaceAll() removes all the occurrences of the specific character from the string.

Using replace() method

Syntax

string.replace(char, '')

char – the character to be removed using the “” blank space

The following code removes the first occurrence of ‘c’ from the given string.

Code

let str = 'abcabc';
let newstr = str.replace('c','');
console.log(newstr);

Output

ababc

Using replaceAll() method

The replaceAll() method replaces all the occurrences of the instance from the given string.

Syntax

string.replaceAll(char, "")

char – the character to be removed using the “” blank space

The below code removes all the occurrences of ‘c’ from the given string.

Code

let str = 'abcabc';
let newstr = str.replaceAll('c','');
console.log(newstr);

Output

abab

Remove Last Character from String

The simplest solution to remove the last character from the string is to use the slice() method.

You need to pass two parameters. The starting index 0 and the second parameter -1.

The negative value in the second parameter removes the character of the string from the end.

Here, -1 removes the last character from the string.

The syntax is as follows.

Syntax

string.slice(0,-1)

The following code describes how to remove the last character ‘s’ from the string ‘alphabets’.

code

let str = 'alphabets';
let newstr = str.slice(0,-1);
concole.log(newstr);

Output

alphabet

JSfiddle

This tutorial is available in this JSFiddle.

Conclusion

In this tutorial, you’ve learned how to remove a text from a string in JavaScript using various methods.

You have also learned how to remove a specific character and the last character from a string in JavaScript.

If you’ve any questions, please comment below.

You May Also Like

Leave a Comment