Dom Dom - 1 year ago 115
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


Answer Source

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).

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