Because that's what you've typed x) !
Here's the details of your expression :
/
RegExp start
(
beginning of an expression group
^
beginning of a string marker
\w
a word character (equivalent of [a-zA-Z0-9]
)
+
quantifier, meaning 1 <= amount < +inf
of what's right before (here a word character)
)
end of an expression group
/
RegExp end
Therefore it matches, from the beginning of a string, any amount(from 1 to +inf) of word-characters and ends capture/match when it encounters another character(not word).
You might have an end of string at the end of your sentences because it only matches the first word of each string on my end (even when there's punctuation).
If you want to make every <p>
's first word bold try this (not optimized for details and understanding sakes):
function boldifyFirstWordOfP(){
var $p_arr = $("p");//cache all P
var p_content_arr;
$p_arr.each(function(index, elem){
p_content_arr[index] = $(this).html();//get all innerHTML content
});
for(var i=0 ; i < p_content_arr.length ; i++){
p_content_arr[i] = p_content_arr[i].replace(/(\w+)/g,"<b>$1</b>");//make first word bold
}
$p_arr.each(function(index, elem){
$(this).html( p_content_arr[index] );
});
}
statement bold. What do you think of this? $("p").each(function() { var change = $(this); change.html(change.text().replace(/(^\w+)/,"$1") ); });
– Robin Varghese Apr 08 '17 at 20:12