Regex capitalize first letter every word, also after a special character like a dash

regex capitalize first letter javascript
regex first letter of each word
regex capitalize first letter of each word c#
dart capitalize first letter of each word
regex capitalize first letter python
regex first letter uppercase rest lowercase
perl uppercase first letter
regex first word

I use this #(\s|^)([a-z0-9-_]+)#i for capitalize every first letter every word, i want it also to capitalize the letter if it's after a special mark like a dash(-)

Now it shows:

This Is A Test For-stackoverflow

And i want this:

This Is A Test For-Stackoverflow

Any suggestions/samples for me?

I'am not a pro, so try to keep it simple for me to understand.

+1 for word boundaries, and here is a comparable Javascript solution. This accounts for possessives, as well:

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"

Capitalize the first character of all words (even when following a , A simple solution is to use word boundaries: #\b[a-z0-9-_]+#i. Alternatively, you can match for just a few characters: #([\s\-_]|^)([a-z0-9-_]+)#i. Regex capitalize first letter every word, also after a special character like a dash (4) +1 for word boundaries, and here is a comparable Javascript solution. This accounts for possessives, as well:

A simple solution is to use word boundaries:

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

Alternatively, you can match for just a few characters:

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

Extract first letter of each word, also after a special character like a , Your problem is an interesting one,but your solution is not a great use of regular expressions, since there's the better option of using the 'word boundary' anchor  Here's another LuaLaTeX-based solution. It tests if the string contains any alphabetical characters, and it does nothing if no alphabetical characters are found. It is not assumed that the first character of the string is a letter-type character. The proposed solution can handle non-ASCII-encoded letters such as ä, Ä, and Å.

Actually dont need to match full string just match the first non-uppercase letter like this:

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

Regular Expressions :: Eloquent JavaScript, This automatically inserts a period after each initial, but that can be With a regex we remove everything from a letter to a space or a hyphen. An identical approach can be used if you need to capitalize following other  Capitalize the first letter of each word in a string except `in`, `the` `of` Hot Network Questions Two people from small group of friends want to have a "meeting" with me.

Try #([\s-]|^)([a-z0-9-_]+)#i - the (\s|^) matches a whitespace character (\s) or the start of the line (^). When you change the \s to [\s-], it matches any whitespace character or a dash.

Regular expression syntax, In addition, backslashes that aren't part of special character codes (like \n ) will be Both of the following expressions match all strings that contain a digit: the uppercase B in the input string, even though the pattern is itself all lowercase. The first argument can also be a regular expression, in which case the first match​  Regex capitalize first letter every word, also after a special character like a dash. Ask Question. Asked 8 years, 3 months ago. Active 7 months ago. Viewed 63k times. 24. I use this #(\s|^)([a-z0-9-_]+)#i for capitalize every first letter every word, i want it also to capitalize the letter if it's after a special mark like a dash(-) Now it shows:

this will make

R.E.A.C De Boeremeakers

from

r.e.a.c de boeremeakers

(?<=\A|[ .])(?<up>[a-z])(?=[a-z. ])

using

    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

[PDF] Regular Expressions: The Complete Tutorial, The regular expression [A-Z][a-z]* matches any sequence of letters The special character * after the closing square bracket specifies to the regular expression {{"A-Z+" }}as matching one or more uppercase letters enclosed by spaces. Therefore, this regular expression matches " BIG " and also matches  regex capitalize first letter Hi, I’ve book with <p class:”body”>word even space</p> on each <p class> but i want (with search & replace with regex) replace every my p class with capitalize first letter <p class:”body”>Word even space</p>

How do I remove the hyphen and capitalise the first letter of each , A regular expression (regex or regexp for short) is a special text string for describing a search pattern. In this tutorial, after introducing a new regex token, I will explain step by step how It will therefore report the first three letters of catfish as a valid match. Finally, \I1 inserts it with the first letter of each word capitalized,. You will see updates in your activity feed. You may receive emails, depending on your notification preferences. Does anyone know how to create a function which accepts a string and capitalizes the first letter in each word of the string, but also forces the other letters to be lowercase? Any advice would be greatly appreciated!!

Regular Expressions Cookbook: Detailed Solutions in Eight , How do I remove the hyphen and capitalise the first letter of each word in this It also needs to be able to work with a single word. This is the best answer because it uses regex, and doesnt use split or a loop to iterate every character in the string. After they gave 100k scholarships to students, Codecademy is now giving  Regex first letter must be capital A Mar 21, 2010 05:49 AM|metalray|LINK. Hi Guys, I browsed through regexlib.com but could not find a regular expression that only checks if the first letter is a capital A. Am I on the right track with something like: ^([A])+/. THanks for any help.

String operations, When hard-coding regular expressions as literal strings in your Python code, you Unicode category Each Unicode code point fits into a single Unicode category. of the word is capitalized ‹\p{Lm}›: A special character that is used like a letter of punctuation character <\p{Pd}>: Any kind of hyphen or dash <\p{Ps }>: Any. Everything after \U, stopping at \E or \e, is converted to uppercase. Similarly, everything after \L, stopping at \E or \e, is converted to lowercase. Alternatively, use \u to uppercase only the first character of what follows, or \l to lowercase only the first character. For example, assume a line with the text "This is a test". :s/\(test\)/\U

Comments
  • Do you also need to capitalize non-ASCII letters (à, ü etc.)? What language are you using?
  • What language's regex are you asking about?
  • toUpperCase is capitalizing the whole word. Here is the solution: s.replace(re, function(x){return x.charAt(0).toUpperCase() + x.slice(1);});
  • @Polopollo, in this case the regex is only returning one letter if it matches but globally. So there is no need for that extra coding and it should work as is.
  • This will not work as OP has asked since a single character would not get capitalized. Just for anybody who comes to this question like I did.
  • I fear this doesn't work: word boundaries include things like '. So don't becomes Don'T
  • @Anderas that's what the negative lookahead is for: (?!\s) checks if it's not a character before whitespace. On the other hand, this fails when a word like don't is followed by a non-whitespace, non-alphanumeric character like a comma, period or exclamation mark. It would be better to use a word boundary in the lookahead: /(\b[a-z](?!\b))/g;
  • Thank you! Works like a charm!
  • @Tim - I took artistic freedom and didn't change the way the OP matches letters - It's possible Simmer wants the letter as output, change their colors or whatnot. Also, didn't gave it that much thought, I only had 4 minutes :P
  • Can someone please add jsfiddle example would be helpful
  • Which language's regex is this for?
  • @JohnK - Both of these are simple enough and should work in all languages. # is a separator here, so your language may need "\\b[a-z0-9-_]+" and an IgnoreCase flag.
  • in js, i've added g like /\b([a-z])/g to capitalize each word
  • i like your lovely answer @StalinGino must say this is the only one i was able to understand.
  • Thank you! Works like a charm