如何编写正则表达式以匹配标题案例句子(例如:我喜欢工作)

前端之家收集整理的这篇文章主要介绍了如何编写正则表达式以匹配标题案例句子(例如:我喜欢工作)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要找到一个匹配每个句子的正则表达式,无论它是否跟随标题大小写(句子的每个单词的第一个字母应该是大写的,并且单词也可以包含特殊字符).
regex101
([A-Z][^\s]*)

Debuggex Demo

描述

1st Capturing group ([A-Z][^\s]*)  
    [A-Z] match a single character present in the list below  
        A-Z a single character in the range between A and Z (case sensitive)
    [^\s]* match a single character not present in the list below
        Quantifier: * Between zero and unlimited times,as many times as possible,giving back as needed [greedy]
        \s match any white space character [\r\n\t\f ]
g modifier: global. All matches (don't return on first match)

Full Sentence

^(?:[A-Z][^\s]*\s?)+$

Debuggex Demo

描述

^ assert position at start of the string
(?:[A-Z][^\s]*\s?)+ Non-capturing group
    Quantifier: + Between one and unlimited times,giving back as needed [greedy]
    [A-Z] match a single character present in the list below
        A-Z a single character in the range between A and Z (case sensitive)
    [^\s]* match a single character not present in the list below
        Quantifier: * Between zero and unlimited times,giving back as needed [greedy]
        \s match any white space character [\r\n\t\f ]
    \s? match any white space character [\r\n\t\f ]
        Quantifier: ? Between zero and one time,giving back as needed [greedy]
$assert position at end of the string
原文链接:https://www.f2er.com/regex/357007.html

猜你在找的正则表达式相关文章