js怎么让英文一出现首字母大写

JavaScript09

js怎么让英文一出现首字母大写,第1张

<script type="text/javascript">

alert("hello world".replace(/(^|\s+)\w/g,function(s){

return s.toUpperCase()

}))

</script>

//a变量你就自己获取,我这儿为你方便你理解我就直接写上了

var a = 'Ajjfioewjfoieji'

if(!(/^[A-Z][A-z0-9]*$/).test(a)){

    alert('请首字母大写')   

}

str="aBcD"

第一条str.toLowerCase().replace(str.slice(0,1),str.slice(0,1).toUpperCase())执行完后,str的值没有发生变化

第二条str= str.toLowerCase().replace(str.slice(0,1),str.slice(0,1).toUpperCase()),str的值变为“Abcd”

第三条执行时,其中str.toLowerCase() =“abcd”,

str.slice(0,1)="A",str.slice(0,1).toUpperCase()="A"

相当于:“abcd".replace("A","A"),

所以结果是"abcd"