正则表达式将每个单词的第一个字母大写,也在特殊字符(如破折号)之后大写
我用它来将每个单词的每个第一个字母大写:
#(\s|^)([a-z0-9-_]+)#i
如果它位于特殊标记(如破折号(-))之后,我希望它也将字母大写。
现在显示:
这是一个针对 stackoverflow 的测试
,我想要这个:
这是一个针对 Stackoverflow 的测试
I use this to capitalize every first letter every word:
#(\s|^)([a-z0-9-_]+)#i
I want it also to capitalize the letter if it's after a special mark like a dash (-).
Now it shows:
This Is A Test For-stackoverflow
And I want this:
This Is A Test For-Stackoverflow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
它可能是最短的:
It could be a shortest:
这将使
REAC De Boeremeakers
从
reac de boeremeakers
使用
this will make
R.E.A.C De Boeremeakers
from
r.e.a.c de boeremeakers
using
+1 表示单词边界,这是一个类似的 Javascript 解决方案。这也解释了所有格:
+1 for word boundaries, and here is a comparable Javascript solution. This accounts for possessives, as well:
一个简单的解决方案是使用单词边界:
或者,您可以只匹配几个字符:
A simple solution is to use word boundaries:
Alternatively, you can match for just a few characters:
如果您想使用纯正则表达式,则必须使用
\u
。要将此字符串:
This Is A Test For-stackoverflow
转换为
This Is A Test For-Stackoverflow
您必须输入:
(.+)-(.+)
捕获“-”之前和之后的值然后要替换它,您必须输入:
$1-\u$2
如果它在 bash 中,您必须输入:
echo "This Is A Test For-stackoverflow" | sed 's/\(.\)-\(.\)/\1-\u\2/'
If you want to use pure regular expressions you must use the
\u
.To transform this string:
This Is A Test For-stackoverflow
into
This Is A Test For-Stackoverflow
You must put:
(.+)-(.+)
to capture the values before and after the "-"then to replace it you must put:
$1-\u$2
If it is in bash you must put:
echo "This Is A Test For-stackoverflow" | sed 's/\(.\)-\(.\)/\1-\u\2/'
实际上不需要匹配完整字符串只需匹配第一个非大写字母,如下所示:
Actually dont need to match full string just match the first non-uppercase letter like this:
对于 JavaScript,这里有一个适用于不同语言和字母表的解决方案:
它匹配前面有字符串
^
开头的任何非空白字符\S
、空白\s
,或任何字符-"'([{
,并将其替换为其大写变体。For JavaScript, here’s a solution that works across different languages and alphabets:
It matches any non-whitespace character
\S
that is preceded by a the start of the string^
, whitespace\s
, or any of the characters-"'([{
, and replaces it with its uppercase variant.我的解决方案使用 javascript
和 es6 + javascript
[a-zÁ-ú]
这里我考虑了字母表中的所有字母,包括大写字母和重音符号。例如:sábado de Janeiro às 19h。 sexta-feira de janeiro às 21 e horas
[a-zÁ-ú]{3, }
所以我要删除一些不够大的字母例如:sábado de Janeiro 于 19 点。 sexta-feira de janeiro às 21 e horas
\b([a-zÁ-ú] {3,})
最后,我只保留所选的完整单词。必须使用 () 来隔离最后一个表达式才能工作。例如:sábado de Janeiro às 19h。 sexta-feira de janeiro às 21 e horas
实现此目标后,我仅将更改应用于以下单词:以小写形式
连接两个
结果:
19 点Sábado de Janeiro。 Sexta-Feira de Janeiro às 21 e Horas
my solution using javascript
with es6 + javascript
[a-zÁ-ú]
here I consider all the letters of the alphabet, including capital letters and with accentuation.ex: sábado de Janeiro às 19h. sexta-feira de janeiro às 21 e horas
[a-zÁ-ú]{3,}
so I'm going to remove some letters that are not big enoughex: sábado de Janeiro às 19h. sexta-feira de janeiro às 21 e horas
\b([a-zÁ-ú]{3,})
lastly i keep only words that complete which are selected. Have to use () to isolate the last expression to work.ex: sábado de Janeiro às 19h. sexta-feira de janeiro às 21 e horas
after achieving this, I apply the changes only to the words that are in lower case
joining the two
result:
Sábado de Janeiro às 19h. Sexta-Feira de Janeiro às 21 e Horas
Python 解决方案:
阅读“正向lookbehind”
Python solution:
read about the "positive lookbehind"
虽然这个针对纯正则表达式解决方案的答案是准确的:
但在使用任何大小写更改运算符:
应使用结束分隔符:
因此最终结果应为:
While this answer for a pure Regular Expression solution is accurate:
it should be noted when using any Case-Change Operators:
the end delimiter should be used:
so the end result should be: