JavaScript 字符函数replace函数 的应用案例
View Code
<script type="text/javascript">
//替换字符
var beginStr = "visit abc,you is abc ,I is ABC,he is aBC";
var afterStr = beginStr.replace("abc", "loaf"); //返回 visit loaf;
// alert(afterStr);
//替换所有字符
var replaceAllStr = beginStr.replace(/abc/g, "loaf"); //替换所有的abc;注意正则不要加引号
// alert(replaceAllStr);
//统一大小写
var text = "javascript Tutorial";
var txt = text.replace(/javascript/i, "JavaScript");
// alert(txt);
//替换左右2层的键值对
var keyStr = "key|value";
var keyStrChange=keyStr.replace(/(\w+)\s*\|\s*(\w+)/,"$2|$1");
//alert(keyStrChange); //返回value|key
var k = "Visul Stiduo2010,office2010";
var kk = k.replace(/(?<=office)2010/i, "$1,---")
alert(kk); //返回
</script>
//替换字符
var beginStr = "visit abc,you is abc ,I is ABC,he is aBC";
var afterStr = beginStr.replace("abc", "loaf"); //返回 visit loaf;
// alert(afterStr);
//替换所有字符
var replaceAllStr = beginStr.replace(/abc/g, "loaf"); //替换所有的abc;注意正则不要加引号
// alert(replaceAllStr);
//统一大小写
var text = "javascript Tutorial";
var txt = text.replace(/javascript/i, "JavaScript");
// alert(txt);
//替换左右2层的键值对
var keyStr = "key|value";
var keyStrChange=keyStr.replace(/(\w+)\s*\|\s*(\w+)/,"$2|$1");
//alert(keyStrChange); //返回value|key
var k = "Visul Stiduo2010,office2010";
var kk = k.replace(/(?<=office)2010/i, "$1,---")
alert(kk); //返回
</script>
个人签名:天行健,君子以自强不息