开发者

"Simple" Text replace function

开发者 https://www.devze.com 2022-12-23 18:03 出处:网络
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 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开发者_如何学JAVA 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\, /, ""));


Using regular expressions the way you are is not really necessary. Instead I'd split the string into tokens and remove the token that matches the search word, if one does:

function removeWord(myString, wordToReplace)
{
    var tokens = myString.split(/\s*,\s*/);  // allow for whitespace around the comma
    var index = $.inArray(wordToReplace, tokens);  

    if (index != -1)
    {
        tokens.splice(index, 1);
    }

    return tokens.join(", "); // Assumes you want a space after each comma
}

var s = "Bart Simpson, Ex-girlfriend, dude, radical";
removeWord(s, "dude"); // returns "Bart Simpson, Ex-girlfriend, radical

If it is possible for the string that you want to search to contain duplicate items, you can use this altered version of the function (indexOf only returns the first index, so a loop is needed to find all of them):

function removeWord(myString, wordToReplace)
{
    var tokens = myString.split(/\s*,\s*/);
    var index;

    while ((index = $.inArray(wordToReplace, tokens)) != -1)
    {
        tokens.splice(index, 1);
    }

    return tokens.join(", ");
}

var s = "Bart Simpson, Ex-girlfriend, dude, radical, dude";
removeWord(s, "dude"); // returns "Bart Simpson, Ex-girlfriend, radical


Not built for robustness, but this does what you want.

function removeWord(myString, wordToReplace)
 {
    return myString.replace(wordToReplace+", ", "");
 };

 var s = "Bart Simpson, Ex-girlfriend, dude, radical" ; 
 s = removeWord(s, "dude") ; // "Bart Simpson, Ex-girlfriend, radical"


If you still want to use a regex, you may try this:

function removeWordFromList(str, word) {
    return str.replace(
        new RegExp("(^|,\\s*)" + escape(word) + "(\\s*,\\s*|$)", "g"),
        "$1") // remove the word
      .replace(/\s*,\s*$/, ""); // remove the trailing comma
}

Still, splitting the string into an array is more intuitive and better if you want to manipulate your list.


var text = ",Bart Simpson ,, ,  Ex-girlfriend, 5 dude ,radical, sth,, dolr, ";

function removeWord(source, word)
{
  return source
    // replace multiple delimiters into one
    .replace(/\s*,[\s*,]*/g, ', ')
    // trim delimiters
    .replace(/^,\s*|,\s*$/g, '')
    // replaces the substring
    .replace( new RegExp('(^|,\\s)'+word+'($|,\\s)'), function(match, left, right) {
      left = left.trim(), right = right.trim();
      return (left+right).length !== (left.length || right.length) ? ', ': '';
    });
}

removeWord(text,'5 dude');

Should bring something like that

"Bart Simpson, Ex-girlfriend, radical, sth, dolr"
0

精彩评论

暂无评论...
验证码 换一张
取 消