我正在替换字符串中的单词而我正在尝试根据'\b' + myword + '\b'
进行匹配
但是,当myword
两端都有非单词字符时,这不起作用。
var myword1 = "testword,";
var myword2 = "(testword";
var testString = "<tag>asdftestword, is a (testwordasdf also (testword </tag> (testword, should be replaced everywhere there is a testword,)";
var rx1 = new RegExp(???(myword1) ,'g');
var rx2 = new RegExp(???(myword2) ,'g');
var result1 = testString.replace(rx1, "replacement");
var result2 = testString.replace(rx2, "replacement");
我希望结果是
result1 == "<tag>asdftestword, is a (testwordasdf also (testword </tag> (replacement should be replaced everywhere there is a replacement)"
result2 == "<tag>asdftestword, is a (testwordasdf also replacement </tag> replacement, should be replaced everywhere there is a testword,)"
答案 0 :(得分:1)
testString.replace( new RegExp('\\b'+myword+'\\b','g'), "replacement");
但是,如果myword
可能包含正则表达式中特殊的字符(例如parens,句点,括号等),那么您需要escape them first:
RegExp.escape = function(text) {
if (!RegExp.sRE) {
var chars = '/.*+?|()[]{}\\'.split('');
RegExp.sRE = new RegExp('\\'+chars.join('|\\'), 'g');
}
return text.replace(RegExp.sRE, '\\$&');
}
var re = new RegExp('\\b'+RegExp.escape(myword)+'\\b','g');
答案 1 :(得分:0)
你不需要正则表达式。只需使用普通的字符串替换:
var myword = "testword,";
var testString = "<tag>asdftestword, is a testword</tag> (testword, should be replaced everywhere there is a testword,)";
var result = testString.replace( myword , "replacement");
答案 2 :(得分:0)
var rx = new RegExp('\\b' + myword ,'g');
var result = testString.replace(rx, 'replacement');
答案 3 :(得分:0)
好的,我终于弄明白了。看起来它太复杂了,只需要一个我需要多个的RegExp!
在行动Here
中查看function EscapeRegex(text) {
if (!RegExp.sRE) {
var chars = '/.*+?|()[]{}\\'.split('');
RegExp.sRE = new RegExp('\\'+chars.join('|\\'), 'g');
}
return text.replace(RegExp.sRE, '\\$&');
}
function ReplaceWholeWord(subjectString, wordtofind, replacement){
var escapedWord = EscapeRegex(wordtofind);
//simplest scenaro, word to find has non-word characters at begining and end - do basic replace
if(regexIndexOf(escapedWord, '[^\\w]', 0) == 0 && regexLastIndexOf(escapedWord, '[^\\w]', 0) == wordtofind.length - 1){
subjectString = subjectString.replace(new RegExp(escapedWord, 'g'), replacement);
}
//word to find begins with non-wordcharacter
else if(regexIndexOf(escapedWord, '[^\\w]', 0) == 0){
var index = regexIndexOf(subjectString, escapedWord+'[^\\w]', index);
while(index > 0){
subjectString = subjectString.substring(0, index) + replacement + subjectString.substring(index + wordtofind.length);
index = regexIndexOf(subjectString, escapedWord+'[^\\w]', index);
}
}
//word to find ends with non-wordcharacter
else if(regexLastIndexOf(escapedWord, '[^\\w]', 0) == wordtofind.length - 1){
var index = regexIndexOf(subjectString, '[^\\w]'+escapedWord, index);
while(index > 0){
subjectString = subjectString.substring(0, index) + replacement + subjectString.substring(index + myword.length + 1);
index = regexIndexOf(subjectString, escapedWord+'[^\\w]', index);
}
}
//word is normal
else{
var index = regexIndexOf(subjectString,'[^\\w]'+escapedWord+'[^\\w]', index);
while(index >= 0){
subjectString = subjectString.substring(0, index + 1) + replacement + subjectString.substring(index + wordtofind.length + 1);
index = regexIndexOf(subjectString,'[^\\w]'+escapedWord+'[^\\w]', index);
}
}
return subjectString;
}