正则表达式将每个单词的第一个字母大写,也在特殊字符(如破折号)之后大写

发布于 2024-11-14 00:03:30 字数 300 浏览 5 评论 0原文

我用它来将每个单词的每个第一个字母大写:

#(\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

走走停停 2024-11-21 00:03:31

它可能是最短的:

const city = "this is a test for-stackoverflow";
const dr = city.replaceAll(/\b[a-z]/g, (p) => p.toUpperCase())
console.log(dr)

It could be a shortest:

const city = "this is a test for-stackoverflow";
const dr = city.replaceAll(/\b[a-z]/g, (p) => p.toUpperCase())
console.log(dr)

只为守护你 2024-11-21 00:03:31

这将使

REAC De Boeremeakers

reac 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

this will make

R.E.A.C De Boeremeakers

from

r.e.a.c de boeremeakers

(?<=\A|[ .])(?<up>[a-z])(?=[a-z. ])

using

    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
记忆里有你的影子 2024-11-21 00:03:30

+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"

+1 for word boundaries, and here is a comparable Javascript solution. This accounts for possessives, as well:

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"
北方。的韩爷 2024-11-21 00:03:30

一个简单的解决方案是使用单词边界

#\b[a-z0-9-_]+#i

或者,您可以只匹配几个字符:

#([\s\-_]|^)([a-z0-9-_]+)#i

A simple solution is to use word boundaries:

#\b[a-z0-9-_]+#i

Alternatively, you can match for just a few characters:

#([\s\-_]|^)([a-z0-9-_]+)#i
夜光 2024-11-21 00:03:30

如果您想使用纯正则表达式,则必须使用\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/'

腹黑女流氓 2024-11-21 00:03:30

实际上不需要匹配完整字符串只需匹配第一个非大写字母,如下所示:

'~\b([a-z])~'

Actually dont need to match full string just match the first non-uppercase letter like this:

'~\b([a-z])~'
非要怀念 2024-11-21 00:03:30

对于 JavaScript,这里有一个适用于不同语言和字母表的解决方案:

const originalString = "this is a test for-stackoverflow"
const processedString = originalString.replace(/(?:^|\s|[-"'([{])+\S/g, (c) => c.toUpperCase())

它匹配前面有字符串 ^ 开头的任何非空白字符 \S、空白\s,或任何字符 -"'([{,并将其替换为其大写变体。

For JavaScript, here’s a solution that works across different languages and alphabets:

const originalString = "this is a test for-stackoverflow"
const processedString = originalString.replace(/(?:^|\s|[-"'([{])+\S/g, (c) => c.toUpperCase())

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.

枕头说它不想醒 2024-11-21 00:03:30

我的解决方案使用 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
  1. [a-zÁ-ú] 这里我考虑了字母表中的所有字母,包括大写字母和重音符号。
    例如:sábado de Janeiro às 19hsexta-feira de janeiro às 21 e horas
  2. [a-zÁ-ú]{3, } 所以我要删除一些不够大的字母
    例如:sábado de Janeiro 于 19 点。 sexta-feira de janeiro às 21 e horas
  3. \b([a-zÁ-ú] {3,}) 最后,我只保留所选的完整单词。必须使用 () 来隔离最后一个表达式才能工作。
    例如:sábado de Janeiro às 19h。 sexta-feira de janeiro às 21 e horas

实现此目标后,我仅将更改应用于以下单词:以小写形式

string.charAt(0).toUpperCase() + w.slice(1); // output -> Output

连接两个

str.replace(/\b(([a-zÁ-ú]){3,})/g, (w) => w.charAt(0).toUpperCase() + w.slice(1));

结果:
19 点Sábado de Janeiro。 Sexta-Feira de Janeiro às 21 e Horas

my solution using javascript

function capitalize(str) {
  var reg = /\b([a-zÁ-ú]{3,})/g;
  return string.replace(reg, (w) => w.charAt(0).toUpperCase() + w.slice(1));
}

with es6 + javascript

const capitalize = str => 
    str.replace(/\b([a-zÁ-ú]{3,})/g, (w) => w.charAt(0).toUpperCase() + w.slice(1));



/<expression-here>/g
  1. [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
  2. [a-zÁ-ú]{3,} so I'm going to remove some letters that are not big enough
    ex: sábado de Janeiro às 19h. sexta-feira de janeiro às 21 e horas
  3. \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

string.charAt(0).toUpperCase() + w.slice(1); // output -> Output

joining the two

str.replace(/\b(([a-zÁ-ú]){3,})/g, (w) => w.charAt(0).toUpperCase() + w.slice(1));

result:
Sábado de Janeiro às 19h. Sexta-Feira de Janeiro às 21 e Horas

别低头,皇冠会掉 2024-11-21 00:03:30

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'

阅读“正向lookbehind

Python solution:

>>> 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'

read about the "positive lookbehind"

百合的盛世恋 2024-11-21 00:03:30

虽然这个针对纯正则表达式解决方案的答案是准确的:

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/'

While this answer for a pure Regular Expression solution is accurate:

echo "This Is A Test For-stackoverflow" | sed 's/\(.\)-\(.\)/\1-\u\2/'

it should be noted when using any Case-Change Operators:

\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.

the end delimiter should be used:

\E

so the end result should be:

echo "This Is A Test For-stackoverflow" | sed 's/\(.\)-\(.\)/\1-\u\E\2/'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文