正則表示式替換單詞首字母大寫
阿新 • • 發佈:2019-01-07
首字母大寫,並讓後續字母小寫
<script type="text/javascript">
function replaceReg(reg,str){str = str.toLowerCase();
return str.replace(reg,function(m){return m.toUpperCase()})
}
var reg = /\b(\w)|\s(\w)/g;
var str = 'share javascript';
var str2 = 'SHARE JAVASCRIPT';
var str3 = 'Test \n str is no good!';
var str4 = 'final test';
document.write(replaceReg(reg,str)+'<br />');
document.write(replaceReg(reg,str2)+'<br />');
document.write(replaceReg(reg,str3)+'<br />');
document.write(replaceReg(reg,str4)+'<br />');
</script>