JSFiddle - React, Tailwind, and code Playground
by Julien Etienne
JavaScript
function processText(input) {
// Split the input into two parts
const text = input.split(/\n-> /)
const tagsSection = text.slice(1)
const content = text[0]
const tagsArray = tagsSection.map(tagPair => {
return tagPair.split('><').map((tag, i) =>{
if(i === 0) return tag + '>'
if(i === 1) return '<' + tag
} )
})
// Replace <> and </> with corresponding tags in the text
let processedText = content;
tagsArray.forEach(([openTag, closeTag]) => {
processedText = processedText.replace(/<>/, openTag).replace(/<\/>/, closeTag);
});
return processedText;
}
// Example usage:
const input = `Risalo specialises in providing comprehensive Localsation <>L10N</> and Internationalisation
<>I18n</> solutions for digital and print media. hello <>How</> Are you.
-> <strong class="abc"></strong>
-> <strong class="def"></strong>
-> <span id="hij"></span>`;
const result = processText(input);
console.log(result);