JSFiddle - React, Tailwind, and code Playground

CSS

span {color: red;}

JavaScript

function htmlEntityChecker(input) {
    var characterArray = ['&', ' '];
    $.each(characterArray, function(idx, ent) {
        if (input.indexOf(ent) != -1) {
            var re = new RegExp(ent, "g");
            input = input.replace(re, '<span>' + ent + '</span>');
        }
    });

    return input;
}


var output = htmlEntityChecker('some words &amp; and spaces &nbsp;');

$('body').html(output);
console.log(output);