0

While replacing the red colored text which surrounded with square brackets of red color.The replaced text should be in black but it turns out be red. How do I fix this

The below code checks for the word with square brackets and if there are, replace those with red color in a textbox(textbox implemented using contenteditable)

if we want to replace those red colored words with other data in a textbox , then while updating it should be in black. If I go one space before of that square bracket word and try to enter something then only it will be in black otherwise it will be in red

  if (event.match(/([^[]+(?=]))/g)) {
  {
  var newTxt = event.split('[');
  let sampleText = event;
  for (var i = 1; i < newTxt.length; i++) {
    let value = newTxt[i].split(']')[0];
    console.log(value, "matchhh");

    sampleText = this.replacecanned(sampleText, `[${value}]`, `<span style="background: yellow;">[${value}]</span>`);

  }
User123
  • 1
  • 1
  • Please correct invalid syntax in the code you posted. – tromgy May 17 '22 at 11:52
  • sorry. I did not understand – User123 May 17 '22 at 11:54
  • Problems start at the second line: **SyntaxError: Unexpected token ']'** – tromgy May 17 '22 at 11:58
  • Sorry .. I updated the code. I forgot to remove the commented lines while posting. – User123 May 17 '22 at 12:08
  • Can you add a minimal prototype like a sandbox and the expected outcome? I am not able to understand the question. – Shivam Sharma May 17 '22 at 12:11
  • Here in this example :https://stackoverflow.com/questions/37139076/change-color-of-specific-words-in-textarea , the first solution shows the area to run the code. there you can check if you type something like " hello, how are you! SELECT" and you could see that SELECT is in yellow. Now if you remove SELECT and update with some other that will also be in yellow that shouldn't happen. If I'm replacing SELECT with some other word, the replaced word should be in black. My code is different and issue Im facing is the same @Shivam Sharma – User123 May 17 '22 at 13:12
  • Why are you tagging this as reactjs, angularjs, vue.js? It will likely only be one of those - on top of which it's irrelevant here (straight vanilla JS) – Kinglish May 17 '22 at 19:40

0 Answers0