JSFiddle - React, Tailwind, and code Playground
by Alexey Ukolov
JavaScript
const $dom = document.createElement('div');
const faq = [
"<p><ul>\n<li>• Download the App from <a href=\"https://itunes.apple.com/US/app?mt=8%29\" target=\"_blank\">AppStore</a> or <a href=\"https://play.google.com/store/apps/details?id=\" target=\"_blank\">Google Store</a></li>\n <li>• Issue a virtual card within the app</li></ul>.</p>",
"<p><ul>\n<li>• Download the App from <a href=\"https://itunes.apple.com/US/app?mt=8%29\" target=\"_blank\">AppStore</a> or <a href=\"https://play.google.com/store/apps/details?id=\" target=\"_blank\">Google Store</a></li>\n <li>• Issue a virtual card within the app</li></ul>.</p>",
"<p><ul>\n<li>• Download the App from <a href=\"https://itunes.apple.com/US/app?mt=8%29\" target=\"_blank\">AppStore</a> or <a href=\"https://play.google.com/store/apps/details?id=\" target=\"_blank\">Google Store</a></li>\n <li>• Issue a virtual card within the app</li></ul>.</p>",
];
const walkDOM = (node, cb) => {
var children = node.childNodes;
for (var i = 0; i < children.length; i++) {
walkDOM(children[i], cb);
}
cb(node);
}
const handleElement = (search, replace, $el) => {
if ($el.nodeType === 3) {
$el.textContent = $el.textContent.replace(search, replace);
}
}
const replace = (search, replace, $dom, html) => {
$dom.innerHTML = faq;
walkDOM($dom, handleElement.bind(null, search, replace));
return $dom.innerHTML;
}
let searchText = 'app';
let searchPattern = new RegExp(searchText, 'gi');
let replaceText = 'Foo';
console.log(faq.map(replace.bind(null, searchPattern, replaceText, $dom)));