JavaScript
Article
By Sam Deering

jQuery remove string from string

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

This is how you might go about using jQuery to remove string from string so to speak. The example below will hopefully clarify what I mean. It uses jQuery.grep() which is a really cool search function – It’s kind of a PHP substring equivalent… kind of anyways.

As always just copy, paste, run using Firebug to test out and experiment.

--ADVERTISEMENT--

(function($) {
var myFruits = "Apples, Bananas, Mangos, Blackberries, Oranges";
myFruits = myFruits.replace(/bMangos(, |$)/gi, "");
 
var myFruits = "Apples, Bananas, Mangos, Blackberries, Oranges";
var result = $.grep(myFruits.split(', '), function(v) { return v != "Mangos"; }).join(', ');
console.log(result);
 
function filterOut(my_str, t) { //string, term
  return $.grep(my_str.split(', '), function(v) { return v != t; }).join(', ');
}
})(jQuery);

//output: Apples, Bananas, Blackberries, Oranges
Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?