Remove Space from Last then Wrap Last Word

by Faisal Khan Janjua

HTML

<div class="slc-sec">
  <h3>Our World is Awesome </h3>
</div>

CSS

.slc-sec h3{
  color:#333;
}
.slc-sec h3 span{
  color:#9F9;
}

JavaScript

jQuery(document).ready(function(){
	jQuery('.slc-sec h3').each(function() { // checking last character is space or not before wrapping
		if(/\s+$/.test(jQuery(this).text())){
			jQuery(this).text(function (_,txt) {
				return txt.slice(0, -1);
			});
		}
	});
	jQuery('.slc-sec h3').each(function() { // wrapping last word into span
		var html = jQuery(this).text();
		var lword = html.substr(html.lastIndexOf(" "));
		var rest = html.substr(0, html.lastIndexOf(" "));
		jQuery(this).html(rest).append("<span>"+lword+"</span>");
	});
});