繁体   English   中英

如何在字符串中将每个单词的首字母大写并将所有空格更改为JavaScript中的下划线

[英]How to capitalize first letter of each word in a string and change all spaces to underscores in JavaScript

我正在尝试在JavaScript中实现一个函数,该函数为给定的输入值提供类似这样的输出

输入: stack overflow

输出: Stack_Overflow

输入: the big bang theory

输出: The_Big_Bang_Theory

我已经编写了将字母大写的代码,但似乎无法弄清楚如何在同一输入上同时调用这两个函数。 我是Java的新手,任何帮助将不胜感激。 我将在此处分享我的代码以进一步阐明

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body>
<input id="myInput" type="text" value="" size="50" />

<pre id="myOutput" type="myInput">type something in the box above</pre>

<script>

String.prototype.capitalize = function(){
return this.toLowerCase().replace( /\b\w/g, function (m) {

return m.toUpperCase();

});
};

String.prototype.replaceAll = function(){
if(!search || !replace){return this;}
return this.replace(/ /g,"_"), function (n){
return n;
});
};

var myInput = document.getElementById('myInput');

var myOutput = document.getElementById('myOutput')

myInput.addEventListener('input', function(e) {
myOutput.innerHTML = this.value.capitalize();


});

myInput.addEventListener('input', function(f)) {
myOutput.innerHTML = this.value.replaceAll();
});

</script>

</body>
</html>

您实际上并没有将任何参数传递给capitalize函数。 我已经稍微修改了您的代码以适应这一点。

// first check to see if `capitalize` doesn't
// already exist on the prototype - don't go overwriting
// native methods :)
if (!('capitalize' in String.prototype)) {
  String.prototype.capitalize = function() {
    return this.toLowerCase().replace(/\b\w/g, function(m) {
      return m.toUpperCase();
    });
  };
}

if (!('replaceAll' in String.prototype)) {

  // pass in search and replace as arguments
  String.prototype.replaceAll = function(search, replace) {
    if (!search || !replace) return this;

    // then just do a replace using the arguments
    return this.replace(search, replace, 'g');
  };
}

var str = 'the big bang theory';
str.capitalize().replaceAll(' ', '_'); // The_Big_Bang_Theory

DEMO

试试这个

var str = 'stack overflow';
str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
});
str= str.replace(' ','_');
alert(str);

https://jsfiddle.net/n6nqpwe6/

UPDATE

我将方法(称为capitalize )嵌入到String类中,并将代码放置在正在运行的演示中:

 String.prototype.capitalize = function() { return this.toLowerCase().replace( /\\b(\\w)(\\w*)( *)/g, function(all, f, r, s) { return f.toUpperCase() + (r?r:'') + (s?'_':''); } ); }; var tests = ['stAck oVerFlow','the bIg bANg theory']; while(t = tests.pop()){ console.log(t, ' -> ', t.capitalize()); } 
 <script src="https://getfirebug.com/firebug-lite-debug.js"></script> 

通古斯

正则表达式使用捕获组来匹配:

  • \\b(\\w) :正则表达式单词的第一个字符(相当于[a-zA-Z0-9_] )。 在单词已经以大写字母或数字开头(或下划线,是否要避免这种情况)之后,我不使用[az]来匹配空格。
  • (\\w*) :正则词的其余部分
  • ( *) :一个或多个空格

然后在结尾处将大写第一个字母,在单词的其余部分(如果有的话)后面加上一个下划线“ _”(如果单词后面实际上有一个或多个空格)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM