简体   繁体   English

正则表达式匹配字符串中的第一个大写字符

[英]Regex match from first uppercase character in string

I have a camelcase string eg thisIsCamelcaseString 我有一个thisIsCamelcaseString字符串,例如thisIsCamelcaseString

I'd like to remove the first lowercase characters from from the string. 我想从字符串中删除开头的小写字符。

To achieve this, I think I'm looking for a regular expression that will match from the first capital letter in the string to the end. 为此,我想我正在寻找一个正则表达式,该字符串从字符串的第一个大写字母到末尾都将匹配。 In this example it would remove this , returning: 在此示例中,它将删除this ,返回:

IsCamelcaseString

This is one way to achieve it: 这是实现它的一种方法:

 console.log('thisIsCamelcaseString'.replace(/^[az]+/, '')); 

This uses the ^ anchor which matches the string from beginning and then searches for the smallcase letters. 这使用从一开始就匹配字符串的^锚,然后搜索小写字母。

You can use this regex 您可以使用此正则表达式

[A-Z]\S+

Demo 演示版

This matches the first capital letter with [AZ] and then any other non-blank characters with \\S+ . 这会将第一个大写字母与[AZ]匹配,然后将所有其他非空白字符与\\S+匹配。

If you want to match only letters you can use 如果您只想匹配字母,则可以使用

[A-Z][a-zA-Z]+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM