Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to select :first-word of <p>?

Tags:

jquery

css

like we select p:first-letter? I know there is no property called p:first-word but if any other way SO user knows.

I don't want to add anything in HTML.

like image 786
Jitendra Vyas Avatar asked Jan 22 '23 14:01

Jitendra Vyas


1 Answers

$('p').each(function(){
    var me = $(this)
       , t = me.text().split(' ');
    me.html( '<strong>'+t.shift()+'</strong> '+t.join(' ') );
  });

This bolds first word.

or

$('p').each(function(){
    var me = $(this);
    me.html( me.text().replace(/(^\w+)/,'<strong>$1</strong>') );
  });
like image 167
ant Avatar answered Jan 30 '23 00:01

ant