简体   繁体   中英

JavaScript replace any word in text with other word

Since this is answered I put the simplest answer here. (Thanks to Bergi)

first making sure that all the special characters are escaped in 'useWord'

useWord.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");

then replace like so:

strIn.replace(new RexExp("\b"+userWord+"\b", "igm",replaceWord);

The original question is answered but since I need to also sometimes replace signs with words (like $ with HKD) the above mentioned answer doesn't work:

/\$/.test("10 $ ") = true
/\b\$\b/.test("10 $ ") = false

Changed my code as there was a simple error in there:

([^A-Z]||^)

should be

([^A-Z]|^) 

Here a detailed description of the code:

The user can specify a word (=userWord variable) and the word to replace it (=replaceWord variable)

for example:

usWord="dr";replaceWord="Dr."

Replaces the word dr with Dr.

It needs to be case insensitive so DR,dR,Dr and dr will be replaced with Dr. Problem is that the user does not know about regular expression so the 'userWord'variable needs to be escaped: (thank you Bergi)

userWord = userWord.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");

Now I tell JavaScript what a "word" is so "drive" would not be replaced and look like "Dr.ive" (edited: original code was wrong, had || instead of | now it's fixed)

([^AZ]|^) means no alphabetic character before the word or the word is at the start of the line. ("dream" and "drive" would match now but not "bandr")

([^AZ]|$) means no alphabetic character after the word or the word is at the end of the line. ("dream" and "drive" don't match anymore now)

r=new RexExp("([^A-Z]|^)("+userWord+")([^A-Z]|$)","igm");

Say a variable strIn is the text that has the words that need to be replaced then now we can start replacing:

strIn=strIn.replace(r,replaceWord);

If strIn = "Julia and dr Ben." userWord="dr" and replaceWord="Dr." then the output is:

"Julia anddrBen."

Oops, I still don't know what the easiest way is to not have the first and last group being replaced (will read the regex tutorial tomorrow, I promise). The solution I came up with so far is:

strIn=strIn.replace(r,function(a,b,c,d){
    return b + replaceWord + d;
});

This didn't work before because I used || instead of |

In my application the userWord and replaceWord is not one singe value but an array but in this case it doesn't matter. Here is the full code (working) Thanks to Jim O'Brien and using $ for the different groups the code could be simplified:

userWord="dr";
replaceWord="Dr.";
strIn="Julie was driving dr David."
userWord = userWord.
    .replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");
r=new RexExp("([^A-Z]|^)("+userWord+")([^A-Z]|$)","igm");
strIn=strIn.replace(r, "$1" + replaceWord + "$2");
return strIn;

Try using word boundary anchors :

var r = new RegExp("\b"+userWord+"\b", "ig");
return strIn.replace(r, replaceWord);

Also, your regex-escaping is not really complete. Use

userWord.replace(/([{}()[\]\\.?*+^$|=!:~-])/g, "\\$1");

(the last few, after the pipe, are more or less optional depending on where you use the escaped string in a regex).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM