<script language="javascript"> var strM = "javascript is a good script language"; //在此我想将字母a替换成字母A alert(strM.replace("a","A"));
</script> //结果,它只替换了首字母。但如果加上正则表达式结果就不一样了!replace()支持正则表达式,它可以按照正则表达式的规则匹配字符或字符串,然后给予替换! <script language="javascript"> var strM = "javascript is a good script language"; //在此我想将字母a替换成字母A alert(strM.replace(/a/,"A")); </script> //但是结果还是没有更改,稍加修改就OK了。
<script language="javascript"> var strM = "javascript is a good script language"; //在此将字母a全部替换成字母A,当正则表达式有"g"标志时,代表将处理整个字符串 alert(strM.replace(/a/g,"A")); </script>