Dom Dom - 4 months ago 31
jQuery Question

First Word in String with jquery

I'm trying to figure out function that will help me wrap first word in string into span.

But unfortunately no luck.

Can anyone help me please?

Many Thanks



One way:

     var me = $(this);
     me.html(me.html().replace(/^(\w+)/, '<span>$1</span>'));

[ source | try it ]

Basically, for each match (something) you replace the first word with itself ($1) wrapped in <span> tags. The character class \w matches letters, digits, and underscores (thus defining what a "word" is - you may need another definition).