JSFiddle - React, Tailwind, and code Playground

by soumya gangamwar

JavaScript

function rePlaceString (data, annotationName, userInput) {

      const startBtagIndex = data.indexOf('<b>');
      const endBtagIndex = data.indexOf('</b>');
      const startItagIndex = data.indexOf('<i>');
      const endItagindex = data.indexOf('</i>');
      let replaceString = '';
      
      let anotherReplaceString = '';

      if ((startBtagIndex > 0 && endBtagIndex > 0) && (startItagIndex > 0 && endItagindex >0)) {
        replaceString = data.substring(0, startItagIndex) + userInput + data.substr(endItagindex, data.length);
        replaceString = replaceString.substring(0, startBtagIndex) + annotationName + replaceString.substr(endItagindex, data.length)
        
        anotherReplaceString = data.replace( /<b>(.*?)<\/b>/, "'" + annotationName + "'");
        anotherReplaceString = anotherReplaceString.replace( /<i>(.*?)<\/i>/, userInput); 
        
      } else if (startBtagIndex > 0 && endBtagIndex > 0) {
       replaceString = replaceString.substring(0, startBtagIndex) + annotationName + replaceString.substr(endItagindex, data.length)
       
        anotherReplaceString = data.replace( /<b>(.*?)<\/b>/, annotationName);
        anotherReplaceString = anotherReplaceString.replace( /<i>(.*?)<\/i>/, userInput);
      } else if (startItagIndex > 0 && endItagindex >0) {
       replaceString = data.substring(0, startItagIndex) + userInput + data.substr(endItagindex, data.length);
       
            anotherReplaceString = data.replace( /<b>(.*?)<\/b>/, annotationName);
        anotherReplaceString = anotherReplaceString.replace( /<i>(.*?)<\/i>/, userInput);
      
      }
      
      
      
   // expected Result 
    return {displayData: replaceString, requestData: anotherReplaceString};
    
}

console.log(rePlaceString(`And I should input <i>Annotaion</i> as <b>inoutUserName</b>`, 'UserName', 'Admin'), '***********')

console.log(rePlaceString(`And I should navigate to <b>Input</b>`, 'UserName', 'Admin'), '!!!!!!!!!!!!!!!!!!!!!!!')