
[英]Javascript method to capitalize the first letter of every word and also every word after hyphen or dash
[英]Regex capitalize first letter every word, also after a special character like a dash
我用它来大写每个单词的每个第一个字母:
#(\s|^)([a-z0-9-_]+)#i
如果它在像破折号( - )这样的特殊标记之后,我也希望它大写字母。
现在它显示:
这是对stackoverflow的测试
我想要这个:
这是对 Stackoverflow 的测试
+1 用于字边界,这是一个可比较的 Javascript 解决方案。 这也解释了所有格:
var re = /(\b[a-z](?!\s))/g;
var s = "fort collins, croton-on-hudson, harper's ferry, coeur d'alene, o'fallon";
s = s.replace(re, function(x){return x.toUpperCase();});
console.log(s); // "Fort Collins, Croton-On-Hudson, Harper's Ferry, Coeur D'Alene, O'Fallon"
如果要使用纯正则表达式,则必须使用\u
。
要转换此字符串:
这是对stackoverflow的测试
进入
这是对 Stackoverflow 的测试
您必须输入: (.+)-(.+)
以捕获“-”之前和之后的值,然后替换它,您必须输入:
$1-\u$2
如果它在 bash 中,您必须输入:
echo "This Is A Test For-stackoverflow" | sed 's/\(.\)-\(.\)/\1-\u\2/'
实际上不需要匹配完整的字符串,只需匹配第一个非大写字母,如下所示:
'~\b([a-z])~'
对于 JavaScript,这是一个适用于不同语言和字母的解决方案:
const originalString = "this is a test for-stackoverflow"
const processedString = originalString.replace(/(?:^|\s|[-"'([{])+\S/g, (c) => c.toUpperCase())
它匹配任何以字符串开头^
、空格\s
或任何字符-"'([{
) 开头的非空白字符\S
,并将其替换为其大写变体。
我的解决方案使用 javascript
function capitalize(str) {
var reg = /\b([a-zÁ-ú]{3,})/g;
return string.replace(reg, (w) => w.charAt(0).toUpperCase() + w.slice(1));
}
带 es6 + javascript
const capitalize = str =>
str.replace(/\b([a-zÁ-ú]{3,})/g, (w) => w.charAt(0).toUpperCase() + w.slice(1));
/<expression-here>/g
[a-zÁ-ú]
这里我考虑了所有字母表,包括大写字母和重音符号。 例如: sábado de J aneiro às 19 h 。 sexta - feira de janeiro às 21 e horas[a-zÁ-ú]{3,}
所以我要删除一些不够大的字母\b([a-zÁ-ú]{3,})
最后我只保留选择的完整单词。 必须使用 () 来隔离最后一个表达式才能工作。完成此操作后,我仅将更改应用于小写单词
string.charAt(0).toUpperCase() + w.slice(1); // output -> Output
加入两者
str.replace(/\b(([a-zÁ-ú]){3,})/g, (w) => w.charAt(0).toUpperCase() + w.slice(1));
结果:
Sábado de Janeiro 至 19 小时。 Sexta -费拉热内卢às 21 e Horas
Python解决方案:
>>> import re
>>> the_string = 'this is a test for stack-overflow'
>>> re.sub(r'(((?<=\s)|^|-)[a-z])', lambda x: x.group().upper(), the_string)
'This Is A Test For Stack-Overflow'
阅读有关“积极的向后看”
尝试#([\s-]|^)([a-z0-9-_]+)#i
- (\s|^)
匹配空白字符 ( \s
) 或行首 ( ^
) . 当您将\s
更改为[\s-]
时,它匹配任何空白字符或破折号。
虽然这个纯正则表达式解决方案的答案是准确的:
echo "This Is A Test For-stackoverflow" | sed 's/\(.\)-\(.\)/\1-\u\2/'
使用任何大小写更改运算符时应注意:
\l Change case of only the first character to the right lower case. (Note: lowercase 'L')
\L Change case of all text to the right to lowercase.
\u Change case of only the first character to the right to uppercase.
\U Change case of all text to the right to uppercase.
应使用结束分隔符:
\E
所以最终结果应该是:
echo "This Is A Test For-stackoverflow" | sed 's/\(.\)-\(.\)/\1-\u\E\2/'
这将使
R.EAC 德布尔米克
从
r.eac de boeremeakers
(?<=\A|[ .])(?<up>[a-z])(?=[a-z. ])
使用
Dim matches As MatchCollection = Regex.Matches(inputText, "(?<=\A|[ .])(?<up>[a-z])(?=[a-z. ])")
Dim outputText As New StringBuilder
If matches(0).Index > 0 Then outputText.Append(inputText.Substring(0, matches(0).Index))
index = matches(0).Index + matches(0).Length
For Each Match As Match In matches
Try
outputText.Append(UCase(Match.Value))
outputText.Append(inputText.Substring(Match.Index + 1, Match.NextMatch.Index - Match.Index - 1))
Catch ex As Exception
outputText.Append(inputText.Substring(Match.Index + 1, inputText.Length - Match.Index - 1))
End Try
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.