正则表达式首字母大写每个单词的首字母,也是一个像破折号之类的特殊字符

时间:2011-06-06 11:38:09

标签: regex letter capitalize

我使用这个#(\s|^)([a-z0-9-_]+)#i来为每个单词的每个首字母大写,我希望它也可以将字母大写,如果它是在一个特殊标记之后,如短划线( - )

现在显示:

This Is A Test For-stackoverflow

我想要这个:

This Is A Test For-Stackoverflow

对我有任何建议/样品吗?

我不是专业人士,所以尽量让我理解。

9 个答案:

答案 0 :(得分:19)

一个简单的解决方案是使用word boundaries

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

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

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

答案 1 :(得分:19)

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

答案 2 :(得分:7)

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

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

答案 3 :(得分:2)

我使用 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 às 19h。 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));

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

答案 4 :(得分:1)

对于JavaScript,这是一种适用于不同语言和字母的解决方案:

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

它匹配任何以字符串\S,空格^或任何字符\s开头的非空白字符-"'([{,以及用大写字母替换它。

答案 5 :(得分:0)

尝试#([\s-]|^)([a-z0-9-_]+)#i - (\s|^)匹配空格字符(\s)或行的开头(^)。当您将\s更改为[\s-]时,它会匹配任何空白字符或短划线。

答案 6 :(得分:0)

这将使

R.E.A.C De Boeremeakers

来自

r.e.a.c 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

答案 7 :(得分:0)

这是我的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'

在此处了解“正向隐藏”:https://www.regular-expressions.info/lookaround.html

答案 8 :(得分:0)

如果要使用纯正则表达式,则必须使用 \u

要转换此字符串:

这是堆栈溢出测试

进入

这是堆栈溢出测试

您必须输入: (.+)-(.+) 捕获“-”之前和之后的值 然后要替换它,您必须输入:

$1-\u$2

如果它是bash,则必须放置:

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

相关问题