"Simple" Text replace function

Posted by YourMomzThaBomb on Stack Overflow See other posts from Stack Overflow or by YourMomzThaBomb
Published on 2010-03-23T19:17:23Z Indexed on 2010/03/23 21:03 UTC
Read the original article Hit count: 492

I have a string which is basically a list of "words" delimited by commas. These "words" can be pretty much any character e.g. "Bart Simpson, Ex-girlfriend, dude, radical"

I'm trying to use javascript, jQuery, whatever i can to replace a word based on a search string with nothing (in essence, removing the word from the list).

For example, the function is defined as such: function removeWord(myString, wordToReplace) {...};

So, passing the string listed above as myString and passing "dude" as wordToReplace would return the string "Bart Simpson, Ex-girlfriend, radical"

Here's the line of code I was tinkering around with...please help me figure out what's wrong with it or some alternative (better) solution:
$myString.val($myString.val().replace(/wordToReplace\, /, ""));

© Stack Overflow or respective owner

Related posts about regex

Related posts about jQuery