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

时间:2021-08-14 01:47:01

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.

这样做除了用空格替换下划线之外的所有事情。我没有真正尝试过任何东西,因为我对regexpressions并不熟悉。

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

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

5 个解决方案

#1


18  

This should do the trick:

这应该是诀窍:

function humanize(str) {
  var 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

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

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

#2


3  

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()});

#3


2  

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);

#4


1  

Another alternative :

另一种选择:

'options_page'.replace(/(^|_)(\w)/g, function ($0, $1, $2) {
    return ($1 && ' ') + $2.toUpperCase();
});

The regular expression :

正则表达式:

(^|_)   beginning of the input OR "_" ($1)
(\w)    a word character (short for [a-zA-Z0-9_]) ($2)
g       all occurences

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

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

#5


0  

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

Like this

只需添加.replace('_','')就像这样

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

#1


18  

This should do the trick:

这应该是诀窍:

function humanize(str) {
  var 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

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

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

#2


3  

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()});

#3


2  

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);

#4


1  

Another alternative :

另一种选择:

'options_page'.replace(/(^|_)(\w)/g, function ($0, $1, $2) {
    return ($1 && ' ') + $2.toUpperCase();
});

The regular expression :

正则表达式:

(^|_)   beginning of the input OR "_" ($1)
(\w)    a word character (short for [a-zA-Z0-9_]) ($2)
g       all occurences

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

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

#5


0  

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

Like this

只需添加.replace('_','')就像这样

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