javascript 用空格替换下划线并大写单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21792367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:42:29  来源:igfitidea点击:

Replace underscores with spaces and capitalize words

javascriptregex

提问by Lee Loftiss

I am attempting to create a way to convert text with lowercase letters and underscores into text without underscores and the first letter of each word is capitalized.

我正在尝试创建一种将带有小写字母和下划线的文本转换为没有下划线的文本的方法,并且每个单词的第一个字母都大写。

ex;

前任;

options_page = Options Page

At this page: How to make first character uppercase of all words in JavaScript?

在此页面:如何使 JavaScript 中所有单词的第一个字符大写?

I found this regex:

我找到了这个正则表达式:

key = key.replace(/(?:_| |\b)(\w)/g, function(key, p1) { return p1.toUpperCase()});

This does everything except replace the underscores with spaces. I have not really tried anything because I am not that familiar with regexpressions.

除了用空格替换下划线外,这可以完成所有操作。我还没有真正尝试过任何东西,因为我对正则表达式不太熟悉。

How can I adjust this regex so it replaces underscores with spaces?

如何调整此正则表达式,以便用空格替换下划线?

回答by marionebl

This should do the trick:

这应该可以解决问题:

function humanize(str) {
  var i, frags = str.split('_');
  for (i=0; i<frags.length; i++) {
    frags[i] = frags[i].charAt(0).toUpperCase() + frags[i].slice(1);
  }
  return frags.join(' ');
}


humanize('humpdey_dumpdey');
// > Humpdey Dumpdey

repl

复制

http://repl.it/OnE

http://repl.it/OnE

Fiddle:

小提琴

http://jsfiddle.net/marionebl/nf4NG/

http://jsfiddle.net/marionebl/nf4NG/

jsPerf:

jsPerf:

Most test data: http://jsperf.com/string-transformations

大多数测试数据http: //jsperf.com/string-transformations

All versions plus _.str: http://jsperf.com/string-transformations/3

所有版本加上 _.strhttp: //jsperf.com/string-transformations/3

回答by Hubert OG

These are two different tasks, so two different regexes is the best solution:

这是两个不同的任务,因此两个不同的正则表达式是最好的解决方案:

key = key.replace(/_/g, ' ').replace(/(?: |\b)(\w)/g, function(key) { return key.toUpperCase()});

To ensure even all capital words is processed. You can add .toLowerCase()before the very first .replace:

以确保处理所有大写单词。您可以.toLowerCase()在第一个之前添加.replace

console.log('TESTING_WORD'.toLowerCase().replace(/_/g, ' ')
.replace(/(?: |\b)(\w)/g, function(key, p1) {
    return key.toUpperCase();    
}));

回答by Arian Acosta

Since Lodash 3.1.0, there's a _.startCase([string=''])method that transforms any case into capitalized words (start case):

从 Lodash 3.1.0 开始,有一种_.startCase([string=''])方法可以将任何大小写转换为大写单词(起始大小写):

_.startCase('hello_world'); // returns 'Hello World'
_.startCase('hello-world'); // returns 'Hello World'
_.startCase('hello world'); // returns 'Hello World'

There are other useful methods in the String section of Lodash. Read the documentation here.

Lodash 的 String 部分还有其他有用的方法。阅读此处的文档。

回答by Alexandre Perez

Here:

这里:

var str = 'Lorem_ipsum_dolor_sit_amet,_consectetur____adipiscing_elit.'
str = str.replace(/_{1,}/g,' ').replace(/(\s{1,}|\b)(\w)/g, function(m, space, letter)
{
  return space + letter.toUpperCase();
})

console.log(str);

回答by leaf

Another alternative:

另一种选择:

camel = "options_page".replace(/(^|_)(\w)/g, function (
(^|_)   beginning of the input OR "_" ()
(\w)    a word character (short for [a-zA-Z0-9_]) ()
g       all occurrences (global)
, , ) { return ( && ' ') + .toUpperCase(); }); console.log(camel);

The regular expression:

正则表达式:

function toCamel(string){
  return string.replace(/(?:_| |\b)(\w)/g, function(){return .toUpperCase().replace('_',' ');});
}

More about regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml.

有关正则表达式的更多信息:http: //www.javascriptkit.com/javatutors/redev.shtml

回答by Sagar Kamble

Simply add .replace('_',' ')

Like this

只需添加 . replace('_',' ')

像这样

##代码##