JSFiddle - React, Tailwind, and code Playground

HTML

<div id="wrapper">Try to span some text on this page span is a paragraph This 123 is a paragraph. 1 This is some span in a div element.</div>

CSS

#wrapper {
	width: 200px;
}

JavaScript

$(document).ready(function(){  
		debugger;
	$('div#wrapper').html(function(){	
		// separate the text by spaces
		var text= $(this).text().split(' ');
		// drop the last word and store it in a variable
		var last = text.pop();
		// join the text back and if it has more than 1 word add the span tag
		// to the last word
		return text.join(" ") + (text.length > 0 ? ' <span class="last">'+last+'</span>' : last);   
	});

});