.replace() contententeditable negative regex

Viewed 32

I'm trying to replace words in my contenteditable if they are not the words "sample" or "test". If they are not those words, I want to color them blue in my contenteditable text box. Right now the regex matches everything and colors everything blue. It works with regex's that do not have the negation or negative lookahead in front of it. Any ideas? Please any advice or suggestions on how to accomplish this would help.

 if(e.keyCode == 32){ 
    var text2 = document.getElementById("editor");
    var text = document.getElementById("editor").innerHTML;
    let regexp = /^(?:(?!\b(sample|test)).)*$/g
    var newText = text.replace(regexp, function(g){
      return "<span class='colorblue'>"+g+"</span>";
    });
    document.getElementById("editor").innerHTML = newText;
    var elem = document.getElementById('editor2');
    this.caretAtEnd(text2);
    
}         
0 Answers
Related