Here is the idea ....
This is a lowercase search case-sensitive version.
var str = 'abc def abc xyz'; var word = 'abc'; var newWord = 'test';
If you need a case-insensitive version, then the code must be changed. Let me know and I can change it for you. (PS. I am doing this, so I will publish it soon)
Update: The following is an example of a lowercase case-sensitive search version
var str = 'abc def AbC xyz'; var word = 'abc'; var newWord = 'test';
NB The above codes are looking for a string. not a whole word (i.e. with word boundaries in RegEx). If a string should be a whole word, it should be recycled.
Update 2:. This is a case-insensitive full word match version with RegEx
var str = 'abc def AbC abcde xyz'; var word = 'abc'; var newWord = 'test'; var pat = new RegExp('(\\b' + word + '\\b)(?!.*\\b\\1\\b)', 'i'); str = str.replace(pat, newWord);
Good luck :)
erosman Apr 17 '14 at 15:20 2014-04-17 15:20
source share