gpt4 book ai didi

javascript - 大写每个单词的第一个字母,\b\w 也适用于 I'm

转载 作者:搜寻专家 更新时间:2023-11-01 04:29:26 24 4
gpt4 key购买 nike

需要将句子中每个单词的第一个字母大写,然而我的正则表达式也将 I'm 中的“m”大写。

完整的表达是这样的:

/(?:^\w|[A-Z]|\b\w)/g

这里的问题(我认为)是 \b\w 将获取单词边界后的第一个字母。我假设单引号表示单词边界,因此也将 I'mm 大写为 I'M

谁能帮我更改表达式以排除单引号后的“m”?

提前致谢。

最佳答案

在语言中间找到一个真正的分词可能有点多
比使用正则表达式字边界复杂。

 ( \s* [\W_]* )           # (1), Not letters/numbers,
( [^\W_] ) # (2), Followed by letter/number
( # (3 start)
(?: # -----------
\w # Letter/number or _
| # or,
[[:punct:]_-] # Punctuation
(?= [\w[:punct:]-] ) # if followed by punctuation/letter/number or '-'
| #or,
[?.!] # (Add) Special word ending punctuation
)* # ----------- 0 to many times
) # (3 end)

var str = 'This "is the ,input _str,ng, the End ';
console.log(str);
console.log(str.replace(/(\s*[\W_]*)([^\W_])((?:\w|[[:punct:]_-](?=[\w[:punct:]-])|[?.!])*)/g, function( match, p1,p2,p3) {return p1 + p2.toUpperCase() + p3;}));

关于javascript - 大写每个单词的第一个字母,\b\w 也适用于 I'm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43575984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com