Javascript 在文本区域中搜索特定单词的步骤
我想在文本区域中搜索一个特定的单词,并用另一个单词替换它 如何在java脚本注册表中执行此操作?Javascript 在文本区域中搜索特定单词的步骤,javascript,Javascript,我想在文本区域中搜索一个特定的单词,并用另一个单词替换它 如何在java脚本注册表中执行此操作? res.replaceAll("(?i)(hello)", "\\*$1\\*"); have tried this. 我猜您想要一个全局替换,但是javascript没有replaceAll,相反,您必须使用带有全局修饰符和捕获组的正则表达式来添加asterix res = res.replace(/(hello)/g, '*$1*'); 您可以执行以下操作: res.replace(/\bw
res.replaceAll("(?i)(hello)", "\\*$1\\*"); have tried this.
我猜您想要一个全局替换,但是javascript没有
replaceAll
,相反,您必须使用带有全局修饰符和捕获组的正则表达式来添加asterix
res = res.replace(/(hello)/g, '*$1*');
您可以执行以下操作:
res.replace(/\bword here\b/g,"another word");
单词边界将只匹配特定的单词。因为您不需要使用正则表达式,只需通过替换方法即可
val = $('textarea').html() ;
val = val.replace("oldword","newword");
$('textarea').html(val);
否则,如果您想通过regex执行此操作,请使用
var str= "new word";
$('textarea').html(str.replace(/new/g, "old"));
}
请将您尝试过的内容发布在网站上。
var str= "new word";
$('textarea').html(str.replace(/new/g, "old"));
}