例如,这个函数
function replaceAll(str,x,y){
return str.split(x).join(y);
}
var x="My cat have a hat a its head.";
replaceAll(x,"at","on")会从这里回来
我的猫头上戴着一顶帽子。
到这个
我的骗子头上有个汉子。
但我想带着
我的猫头上戴着一顶帽子。
发布于 2014-02-17 19:47:37
用word边界regex像这样定义函数:
function replaceAll(str,x,y){
return str.split(new RegExp("\\b"+x+"\\b")).join(y);
}然后:
repl = replaceAll(x,"at","on")
//=> My cat have a hat on its head.https://stackoverflow.com/questions/21837896
复制相似问题