• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Remove Word From String In JavaScript

Remove Word From String In JavaScript

May 9, 2022 Leave a Comment

To remove a word from a string in JavaScript, the easiest way to do this is to use the JavaScript String replace() method.

someString.replace("theWord", "");

Let’s see this in a full example.

var someString = "This is a short sentence."

someString = someString.replace("short", "");

console.log(someString);

#Output:
This is a sentence.

Let’s take a look at another example.


When using string variables in JavaScript, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove words from a string variable.

We can easily remove word(s) from a string in JavaScript.

The easiest way to remove a word from a string using JavaScript is with the JavaScript String replace() function.

The replace() function takes two arguments: the substring we want to replace, and the replacement substring. In this case, to remove a word, we pass the word (“word”) as the first argument, and an empty string (“”) as the second argument.

Remove All Instances of a Word From a String In JavaScript

Let’s say we wanted to remove all instances of a word instead of just the first occurrence of that word. We would have to change our code above slightly. We will use some example text from our About page.


var someString = "This string with have the word string in it three times to show the effect of removing the word string."

someString = someString.replace(/string/g, "");

console.log(someString);

#Output:
This with have the word in it three times to show the effect of removing the word .

Hopefully this article has been useful for you to learn how to remove a word from a string in JavaScript.

Other Articles You'll Also Like:

  • 1.  How to Use JavaScript to Change Button Text
  • 2.  Uncheck a Radio Button Using JavaScript
  • 3.  Using JavaScript to Resize an Image
  • 4.  Add Days to a Date Using JavaScript
  • 5.  Examples Using the JavaScript += Addition Assignment Operator
  • 6.  Add Minutes to a Date Using JavaScript
  • 7.  How to Repeat a String in JavaScript
  • 8.  Remove Commas From Array in JavaScript
  • 9.  Using JavaScript to Subtract Days From a Date
  • 10.  Using JavaScript to Capitalize the First Letter of a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy