在Javascript中替换多个单词

在Javascript中替换多个单词,javascript,Javascript,有人知道有没有比这更有效的方法来用一个单词替换多个单词 variableName.replace(/\small/g , 'word').replace(/\medium/g , 'word').replace(/\large/g , 'word').replace(/\x_large/g , 'word'); 是-修改模式以接受多个选项 variableName.replace(/\b(word1|word2|word3)\b/g, 'word'); 使用单词边界也很重要\b以确保单词本身

有人知道有没有比这更有效的方法来用一个单词替换多个单词

variableName.replace(/\small/g , 'word').replace(/\medium/g , 'word').replace(/\large/g , 'word').replace(/\x_large/g , 'word');

是-修改模式以接受多个选项

variableName.replace(/\b(word1|word2|word3)\b/g, 'word');
使用单词边界也很重要\b以确保单词本身匹配,而不是作为其他单词的一部分,例如bridge vs.Abred

您可以简单地使用|

让str='w1你好w2你好w3' 设op=str.replace/\bw1 | w2 | w3\b/g, console.logop