How to remove text from string in JavaScript ?

JavaScript is widely used language. We may sometime need to remove text from string to get a desired output from certain API calls or even within an JavaScript APP. Answers to your question on how to remove text from string in JavaScript are mentioned below.

Removing Text using String.prototype.replace()

Using this method does not alter the original string but returns a new string.
For instance:

var str = "Hello World!";
var newStr = str.replace('Hello', 'Hy');
console.log(newStr);  // Prints: Hy World!

In order to remove all occurrences of the word “Hello” we can write following line.

str.replace(/Hello/g, 'Hy');

Removing Text in JavaScript using slice() method

The slice()  method extracts parts of a string and returns the extracted parts as a new string. It takes two parameter which specify the parts of the string to be extracted.

Syntax: string.slice(start, end)

start: Specifies the position to begin the extraction. This parameter is mandatory. The first character is at position 0.

end: Specifies the position where the extraction should end. This parameter is optional and if not provided, slice() selects all characters from the start-position to the end of the string.

Here’s an example on how to remove text from string in JavaScript using slice()  method:

var str = "Hello World!";
var result = str.slice(0); 
console.log(result); // Prints: Hello World! as the starting position is 0.

Extract from position 2

var str = "Hello World!"
var result = str.slice(2);
console.log(result); //Prints: llo World!

Providing both the parameters and extracting the string

var str = "Hello World!";
var result = str.slice(2, 8);
console.log(result); //Prints: llo Wo

We can give negative value to start parameter in order to start from the end of the string.

var str = "Hello World!";
var result = str.slice(-1);
console.log(result); //Prints: !

Custom method to delete word from string in JavaScript

String.prototype.removeWord = function(searchWord){
    var str = this;
    var n = str.search(searchWord);
    while(str.search(searchWord) > -1){
        n = str.search(searchWord);
        str = str.substring(0, n) + str.substring(n + searchTerm.length, str.length);
    }
    return str;
}

//Using above method
var string = "Today is is Monday";
string.removeWord('is'); // Returns: Today is Monday

JavaScript code to remove text from string and extract only Number

For this we’ll be using Regular Expression and match that to the given string and get the output as number. Here’s an example code snippet on how to remove text from string in JavaScript and extract number from the string.

Number(("data-123").match(/\d+$/));

// strNum = 123

Here’s what the statement above does…working middle-out:

  1. str.match(/\d+$/)  – returns an array containing matches to any length of numbers at the end of str. In this case it returns an array containing a single string item [‘123’].
  2. Number()  – converts it to a number type. Because the array returned from .match() contains a single element Number() will return the number.

How to remove text inside brackets using Regular Expressions in JavaScript

Let’s jump straight into the code to learn how to remove text inside brackets using Regular Expressions in JavaScript.

"Hello, this is Mike (example)".replace(/ *\([^)]*\) */g, "");

//Result
"Hello, this is Mike"

Here, we are using regular expression / *\([^)]*\) */g that finds the characters inside brackets (in this case small round brackets) and replace them with empty string.

References:

How to remove text from a string in JavaScript?

JavaScript/Regex : Remove Text inside Brackets

9 thoughts on “How to remove text from string in JavaScript ?”

  1. on the line

    str = str.substring(0, n) + str.substring(n + searchTerm.length, str.length);

    I think you may have meant searchWord.length rather than searchTerm.length

    Reply
  2. What if i have code like this?
    var string = “this is something i (want to] say because it’s (funny] Confusing”;
    How do i remove everything between the “(” and the “]” with it coming out like this:
    “this is something i Confusing”

    Reply
  3. How would i replace it if my string is like this?
    “lorem ipsum was walking along when {all of a sudden) he met dolor sit amet. then dolor sit amet {dropped dead) ”
    How do i replace between “{” and “)” without removing everything between the first “{” and the last”)”

    Reply

Leave a Comment

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

%d bloggers like this: