js中replace的用法(两种常用举例,还有好多用法不一一列举)

1、替换特定字符

<html>
<body>

<script type="text/javascript">

var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/,"W3School"))

</script>
</body>
</html>
2、全局替换

<html>
<body>

<script type="text/javascript">

var str="Welcome to Microsoft! ";
str=str + "We are proud to announce that Microsoft has ";
str=str + "one of the largest Web Developers sites in the world.";
document.write(str.replace(/Microsoft/g, "W3School"));

</script>

</body>
</html>

 

posted on 2017-02-11 15:45  2015熊出没  阅读(224)  评论(0编辑  收藏  举报