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.

Basic Example

string.substring(startvalue, endvalue);

This tutorial teaches various methods to remove text from a JavaScript string and how to remove specific characters 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.

Use this method when the text indices to be extracted are unknown.

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

Code

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

let str = 'this is the original string';

let str1 = str.substr(5,10);

console.log(str1);

Output

is the ori

Using substring() method

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

The start index and end index are passed as parameters.

Use this method 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.

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.
  • 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

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.

Syntax

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

Code

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

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 removes all the occurrences of a specific text from the given string by replacing it with a blank space.

Syntax

string.replace(/text to remove/g, '')
  • g – global flag to be used with regex

Code

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

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

Output

sells shells, sells pearls

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
  • 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

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

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.

  • Pass two parameters. The starting index is 0, and the second parameter is -1.
  • The negative value in the second parameter removes the character of the string from the end.

Syntax

In the following example, -1 removes the last character from the string.

string.slice(0,-1)

code

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

let str = 'alphabets';

let newstr = str.slice(0,-1);

concole.log(newstr);

Output

alphabet

JSfiddle

This tutorial is available in this JSFiddle.

Additional Resources

Leave a Comment