「JavaScript入門」stringObject.replace() のサンプル

サンプルコード1
<script type=”text/javascript”>

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

</script>

結果
Visit W3School!

サンプルコード2
<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>
結果
Welcome to W3School! We are proud to announce that W3School
has one of the largest Web Developers sites in the world.