RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex to capitalise the first letter

/^(\w)/

Capture the first word character so a replacement can upper-case it. Combined with the m and g flags it does the same to every line. Note the caveat: for scripts where a character upper-cases into two, only the language's own case functions are correct.

What each part does

^start of the string
(\w)capture that first word character

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
hellomatchesh
hello worldmatchesh
1abcmatches1
hellono match
(empty string)no match

The mistake to avoid

Assuming it works for every alphabet. The German ß upper-cases to SS — two characters — and no single-character replacement handles that.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers