本文介绍了将句子改为大写案例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 我是一个javascript新手,我会坚持到一个地方。 我有一个要求我在哪里将在变量中得到一个句子 示例 var v1 ="这是一个句子 现在我必须将句子更改为大写的情况,其中每个单词的第一个 字母将在大写字母中 所以对于上面的例子输出应该是 这是一个句子 我找到了一些可以将大小写改为大写的脚本或 小写,但我无法为此提出解决方案。 还有一件事,我将获得的字数没有限制。我可能会得到一个字甚至十个字。 我正在寻找适用于所有场景的解决方案, 问候, Rayne Hello, I am a javascript newbie and I''m stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence" Now I have to change the sentence to Capitalize case where the firstalphabet of every word will be in caps So for above example the output should be This Is A Sentence I have found some scripts that can change case to uppercase orlowercase but I''m not able to come up with a solution for this. One more thing, there is no limit on the number of words that I''ll getin the sentence. I may get one word or even ten words. I''m looking for a solution that will work for all scenarios, Regards,Rayne推荐答案 v1 = v1.replace(/ \b(\ w)/ g,function(s,c){ 返回c.toUpperCase(); }); BTW,你的问题看起来像是典型的家庭作业。如果那个' 的情况:让其他人解决你的初学者作业不是一个聪明的想法,如果你想学习这门语言或者必须通过以后考试。如果这不是作业,请忽略。 - 康拉德 v1 = v1.replace(/\b(\w)/g, function (s, c) {return c.toUpperCase();}); BTW, your question looks like a typical homework assignment. If that''sthe case: letting other people solve your beginner assignments is not anot a clever idea, if you want to learn the language or have to passexams later. If this wasn''t homework, please disregard.- Conrad 你的意思是_letter_; *不是*字母,这是一组字母。 < http://en.wikipedia.org/wiki/Alphabet> You mean _letter_; *not* alphabet, which is a set of letters. <http://en.wikipedia.org/wiki/Alphabet> v1 = v1。替换(/(^ | \)([az])/ g, 函数(m,p1,p2){ 返回p1 + p2.toUpperCase( ); }); 您可以根据自己的需要调整角色等级。 PointedEars - var bugRiddenCrashPronePieceOfJunk =( navigator.userAgent.indexOf(''MSIE 5'')!= -1 && navigator.userAgent.indexOf(''Mac'')!= -1 )// Plone,register_function.js:16 v1 = v1.replace(/(^|\s)([a-z])/g,function(m, p1, p2) {return p1 + p2.toUpperCase();}); You may adapt the character class to fit your needs.PointedEars--var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf(''MSIE 5'') != -1&& navigator.userAgent.indexOf(''Mac'') != -1) // Plone, register_function.js:16 a)正则表达式,但我不知道如何使用它们。 b)1:将字符串拆分为单词; 2:首字母大写; 3:连接 单词到字符串。 1:查找var wordarray = []; wordarray = v1.split('''')将执行 2:对于所有k:wordarray [k] = wordarray [k] .charAt(0).toUpperCase()+ wordarray [k] .substr(1); 3:查看连接函数:output = wordarray.join(''''); Tom a) Regular Expressions, but I don''t know how to use them. b) 1:Split string into words; 2:capitalize first letters; 3:concatenatewords into string.1: look up what var wordarray = []; wordarray = v1.split('' '') will do2: for all k: wordarray[k] = wordarray[k].charAt(0).toUpperCase() +wordarray[k].substr(1);3: check out the join function: output = wordarray.join('' ''); Tom 这篇关于将句子改为大写案例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 07:18