0

I've found regular expressions that capitalize the first letter in a sentence. But does anyone know a regex that capitalizes the first letter inside a tag, including URL and image attributes (e.g. title="antelope" or alt="antelope").

I used another regex to change all my image paths to lower case, and it zapped a bunch of my tags as well (alt, title, h2, etc.). So now I'd like to get a head start fixing them by capitalizing the first letters.

I'm working on a Mac, using Dreamweaver and TextWrangler as my text editors.

Before...

alt="antelope" title="antelope" <h2>antelope

After...

alt="Antelope" title="Antelope" <h2>Antelope
David Blomstrom
  • 1,379
  • 3
  • 16
  • 29

2 Answers2

0

Regex

(="\w|>\w)

Replace Regex

\U$1\E

Description: This will work for your example, depending on the regex engine you are using.

Regular expression visualization

Debuggex Demo

abc123
  • 16,261
  • 6
  • 46
  • 76
  • @aliteralmind `\E` stops the uppercasing. Since the only thing in the group will be symbols `\U` doesn't do anything to them...`\u` requires me to break my regex into many many more groups. – abc123 Apr 12 '14 at 22:42
  • Hmmmm...None of the above regexes are working for me in Dreamweaver or TextWrangler. Maybe I just need a third text editor. ;) I think Notepad uses a different regex engine. (I'm on a Mac, though.) I'll check out the StackOverflow Regex FAQ. – David Blomstrom Apr 12 '14 at 22:54
0

This replaces the value in parameters in a url. NOT in html, as I now see that is what you mean. Oh well.

  • Find what: (\?|\&)([a-z_]+=)([a-z])([^&]+)
  • Replace (all) with: $1$2\u$3$4

Free spaced:

(\?|\&)

Capture group 1: Either the literal question mark or ampersand.

([a-z_]+=)

Capture group 2: One or more of any lowercase letter or underscore, followed by the equals sign.

([a-z])

Capture group 3: The first letter in the value of the url parameter. Note this does not even notice parameters whose values don't start with a letter.

([^&]+) 

Capture group four: Every other character in the value. Or more specifically, one or more of any character as long as it's not an ampersand. This is a negative character class.

The \u in the replace-with is an option in TextWrangler (and in TextPad, which is what I use...so TextWrangler might also use the Boost regex engine) replacement that uppercases the immediately-following character. I'm not sure if this would work if capture groups 3 and 4 were merged.

Try it (although it doesn't have the \u option.)


Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference. There's a lot of helpful information in it, including a list of online regex testers (in the bottom section), so you can try things out yourself. All the links in this answer come from the FAQ.

Community
  • 1
  • 1
aliteralmind
  • 18,274
  • 16
  • 66
  • 102