简体   繁体   English

如何正确结合正则表达式和替换。 大写单词的方法

[英]How to correctly combine Regex and the replace. method to Capitalize a word

I am trying to capitalize the first word of a string 我正在尝试大写字符串的第一个单词

 function titleCase(str) { var newStr = str.split(" "); //TeSt 10 array newStr for (var i = 0; i < str.length; i++) { // (newStr[i][0].replace(new RegExp("[az]"), new RegExp("[AZ]"))) } } titleCase("I´ma little tea pot") 

I am very confused about Regex so the code I know is wrong but I´d like to know if it's possible to do it as closely similarly like this. 我对Regex感到非常困惑,所以我知道的代码是错误的,但是我想知道是否有可能像这样非常紧密地做到这一点。 So basically once the first letter of each word has been located (newStr[i][0]) I want to .replace(the noncaps to caps) . 因此,基本上,一旦每个单词的第一个字母都已定位(newStr [i] [0]),我就想将.replace(the noncaps to caps)

I know the RegExp is probably very wrong, but, could something like this work? 我知道RegExp可能是非常错误的,但是,这样的工作可以吗? Combining replace and instead of putting the literal string you want to replace (because that is already done in newStr[i][0] ) doing it with Regex 结合使用replace而不是放置要替换的文字字符串(因为newStr[i][0]已经完成了newStr[i][0] ),使用Regex

You could match the first character and use the replacer function: 您可以匹配第一个字符并使用替换器功能:

 newStr[i] = newStr[i].replace(/[a-z]/, it => it.toUpperCase());

But why don't you just do: 但是,为什么不这样做:

 newStr[i] = newStr[i][0].toUpperCase() + newStr[i].slice(1);

You could also just use a Regex to replace all occurences: 您也可以使用正则表达式替换所有出现的事件:

"test test".replace(/(^| )[a-z]/g, it => it.toUpperCase()))

Explanation: 说明:

(^| ) -> is at the beginning of the string (^) or (|) is after a whitespace ( ) (^ |)->在字符串(^)的开头或(|)在空格()之后

[az] -> is a letter [az]->是字母

/g -> apply to all found characters / g->应用于所有找到的字符

The replace function has two parameters: pattern and replacement. replace功能具有两个参数:模式和替换。 Pattern is what to search for, and can be either a fixed string, or a regular expression. 模式是要搜索的内容,可以是固定字符串或正则表达式。 The replacement can be either a string (optionally with group placeholders that can be copied as-is into the replacement), or a function (that is much more flexible in what replacement it can produce). 替换可以是字符串(可以选择带有组占位符,可以按原样复制到替换中),也可以是一个函数(在生成替换内容方面更加灵活)。 It cannot be a regular expression, which is the reason your code can't work. 它不能是正则表达式,这就是您的代码无法工作的原因。

A simple copy-paste cannot turn a lowercase letter into an uppercase, so you have to go with a replacer function: 一个简单的复制粘贴不能将小写字母变成大写字母,因此必须使用替换函数:

newStr[i] = newStr[i].replace(new RegExp("[a-z]"),
    function(match) { return match.toUpperCase(); }
)

However, you can avoid even splitting if you just let the regular expression look for the word breaks: \\b\\w will look for word characters (az, AZ, 0-9 or _) that follow a word boundary (ie come either at the start of the string, or after a non-word character). 但是,如果只让正则表达式查找换行符,甚至可以避免拆分: \\b\\w将查找跟随单词边界(例如,出现在两个字符处)的单词字符(az,AZ,0-9或_)字符串的开头,或非单词字符之后)。 It does not matter that you also look for digits and underscores, as they will not change by uppercasing. 您也可以查找数字和下划线,因为它们不会因大写字母而变化,这没关系。 You can also use the new function syntax, and the regexp literal syntax, to shorten the code: 您还可以使用新的函数语法和regexp文字语法来缩短代码:

str = str.replace(/\b\w/, alnum => alnum.toUpperCase());

Even more shortly: 甚至更短时间:

 console.log( "I´ma little tea pot".replace(/\\b\\w/g, word => word.toUpperCase()) ); 

Or, with a function: 或者,具有以下功能:

function titleCase(str) {
  return str.replace(/\b\w/g, word => word.toUpperCase());
}
titleCase("I´m a little tea pot")

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

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