JSFiddle - React, Tailwind, and code Playground

RegEx for NBSP replacement

by Slava

HTML

test a one1.
 test a two.
  test a three.
   test a  four.
 test a   five. test six. ,. " " seven ' ' eight '' nine <code> ten</code>
&nbsp;
LoremIspum.&nbsp;LoremIspum&nbsp;LoremIspum&nbsp;LoremIspum

CSS

body{
  white-space: pre-wrap;
  word-wrap: break-word;
  word-break: break-all;
}
.marker{
  background:rgba(255,0,0,0.1);
  padding-left:5px;
}

JavaScript

var NBSP = String.fromCharCode(0x00A0);
var marker = '<span class="marker"></span>';
//document.body.innerHTML = document.body.innerHTML.replace(/ /g, NBSP);
//document.body.innerHTML = document.body.innerHTML.replace(/(\S)( )(?=\S)/g, '$1A$3');
document.body.innerHTML = document.body.innerHTML.replace(/ /g, NBSP).replace(/(\S)(\u00A0)(?=\S)/g, '$1'+marker);
//document.body.innerHTML = document.body.innerHTML.replace(new RegExp('(\\S)('+ NBSP +')(?=\\S)', 'g'), '$1A$3');
//document.body.innerHTML = document.body.innerHTML.replace(new RegExp('(\\S)( )(\\S)', 'g'), '$1A$3');