Chris Brauckmuller Chris Brauckmuller - 1 year ago 149
jQuery Question

jQuery match first letter in a string and wrap with span tag

I'm trying to get the first letter in a paragraph and wrap it with a

tag. Notice I said letter and not character, as I'm dealing with messy markup that often has blank spaces.

Existing markup (which I can't edit):

<p> Actual text starts after a few blank spaces.</p>

Desired result:

<p> <span class="big-cap">A</span>ctual text starts after a few blank spaces.</p>

How do I ignore anything but
? Any help would be greatly appreciated.

Answer Source
$('p').html(function (i, html)
    return html.replace(/^[^a-zA-Z]*([a-zA-Z])/g, '<span class="big-cap">$1</span>');


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download