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 = '';
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)
} else if (startBtagIndex > 0 && endBtagIndex > 0) {
replaceString = replaceString.substring(0, startBtagIndex) + annotationName + replaceString.substr(endItagindex, data.length)
} else if (startItagIndex > 0 && endItagindex >0) {
replaceString = data.substring(0, startItagIndex) + userInput + data.substr(endItagindex, data.length);
}
// expected Result
return {displayData: replaceString, requestData: `And I should input '${annotationName}' as '${userInput}'`};
}
console.log(rePlaceString(`And I should input <i>Annotaion</i> as <b>Input</b>`, 'UserName', 'Admin'), '***********')
console.log(rePlaceString(`And I should navigate to <b>Input</b>`, '', 'Admin'), '!!!!!!!!!!!!!!!!!!!!!!!')