例如:
HOW ARE YOU? I AM FINE, THANK YOU. WHERE ARE YOU FROM? I AM FROM CHINA. WHAT ABOUT YOU?或者:how are you? i am fine, thank you. where are you from? i am from china. what about you?转换成正常句子的大小写,就是句子的第一个单词,首字母是大写的,句子的其余部分是小写的:
How are you? I am fine, thank you. Where are you from? I am from China. What about you?我历遍整个互联网,只找到下面的JavaScript例子。第一个例子,是把整篇文章的第一个字母变成大写,其余的是小写。(现实中没什么用)。第二个例子,是把所有的单词的首字母都变成大写,类似英文标题。但它们都不是我想要的。谁能帮我修改一下,完成我要的功能?
function wordUpperCase(word){
var wLength = word.length;
if(wLength > 0){
var ss = (word.substring(0,1).toUpperCase()) + (word.substring(1,wLength).toLowerCase()+" ");
}
return ss;
}function titleUpperCase(title){
var upperTitle = '';
var array = title.split(' ');
for(i = 0 ;i < array.length ; i++){
upperTitle += wordUpperCase(array[i]);
}
return upperTitle;
}
HOW ARE YOU? I AM FINE, THANK YOU. WHERE ARE YOU FROM? I AM FROM CHINA. WHAT ABOUT YOU?或者:how are you? i am fine, thank you. where are you from? i am from china. what about you?转换成正常句子的大小写,就是句子的第一个单词,首字母是大写的,句子的其余部分是小写的:
How are you? I am fine, thank you. Where are you from? I am from China. What about you?我历遍整个互联网,只找到下面的JavaScript例子。第一个例子,是把整篇文章的第一个字母变成大写,其余的是小写。(现实中没什么用)。第二个例子,是把所有的单词的首字母都变成大写,类似英文标题。但它们都不是我想要的。谁能帮我修改一下,完成我要的功能?
function wordUpperCase(word){
var wLength = word.length;
if(wLength > 0){
var ss = (word.substring(0,1).toUpperCase()) + (word.substring(1,wLength).toLowerCase()+" ");
}
return ss;
}function titleUpperCase(title){
var upperTitle = '';
var array = title.split(' ');
for(i = 0 ;i < array.length ; i++){
upperTitle += wordUpperCase(array[i]);
}
return upperTitle;
}
解决方案 »
- 如何实现双层结构体数组不定长度操作
- 这段js代码是什么意思 ?跟木马有关吗?
- qq农场 JavaScript特效
- 关于prototype的问题
- 一个页面 内容用dom动态添家 怎么把它一直定位在页面最底部?谢谢
- 关于快速提交表单客户端的验证
- 一个莫名其妙的问题
- 在<a href="javascript:XXXXXXXXXXXX">按钮</a>的连接中,我想在"javascript:"语句里面加上什么可以实现和普通连接的效果?(今天结)
- 求助!向前辈求Treeview源码
- 在applet中调用javascript的问题
- 现在有什么好的方法获取客户端机器的MAC地址码
- 问什么我javascript return false 还能提交?
var toSen=function(str){
return str.toLowerCase().replace(/((?:[.?!]|^) ?[a-z])/g,function($,$1){
return $1.toUpperCase()
})
};
var nstr=toSen(str);
alert(nstr);
<script type="text/javascript">
var str = "HOW ARE YOU? I AM FINE, THANK YOU. WHERE ARE YOU FROM? I AM FROM CHINA. WHAT ABOUT YOU?";
function toUpper(str) {
return str.toLowerCase().replace(/(^|\?|\.)(\s*)([a-z])/g, function($0, $1, $2, $3) {
return $1 + $2 + $3.toUpperCase();
});
}
alert(toUpper(str));
</script>
var str = 'i am happy to join with you today in what will go down in history as the greatest demonstration for freedom in the history of our nation.\r\nfive score years ago, a great american, in whose symbolic shadow we stand today, signed the emancipation proclamation. this momentous decree came as a great beacon light of hope to millions of negro slaves who had been seared in the flames of withering injustice. it came as a joyous daybreak to end the long night of their captivity.\r\nbut one hundred years later, the negro still is not free. one hundred years later, the life of the negro is still sadly crippled by the manacles of segregation and the chains of discrimination. one hundred years later, the negro lives on a lonely island of poverty in the midst of a vast ocean of material prosperity. one hundred years later, the negro is still languished in the corners of american society and finds himself an exile in his own land. and so we\'ve come here today to dramatize a shameful condition.';
//在网页中通过innerText得到的数据里换行符为 \r\n,这是 windows默认的换行符。
//alert(str.toLowerCase())
setp_Up(str);
}
function setp_Up(temp){
temp = temp.toLowerCase();
temp = temp.replace(/(^|\n|[\.?!]\s?)([a-zA-Z])/g, function($0, $1, $2) { return $1 + $2.toUpperCase();});
alert(temp);
}
setUp();
.firstLetterUpCase{
text-transform:lowercase;
}不过之前要将文本内的所以字母都改为小写txt.toLowerCase();不过有一个小缺憾:如果是这样: aaa你好a会成这样:Aaa你好A;就是说不认中文,如果有,就会被认为是空格一样。
text-transform:capitalize;