I'm trying to do the following treatment in a string in javascript using ER (Regular Expression):
With this input: um <b>negrito<b>negrito interno</b>externo</b> aqui <b>negrito</b> <i>italico</i>.
, I'd like to get the tag
<b>
complete, with all its content up to its closing </b>
, which is the expected result: <b>negrito<b>negrito interno</b>externo</b>
and <b>negrito</b>
.
But I am not able to consider that a tag
can contain another same internally, and I was able to reach the maximum until this result (which does not consider the possibility of a tag
same internally, as can be seen in the first result where is <b>negrito<b>negrito interno</b>
instead of <b>negrito<b>negrito interno</b>externo</b>
:
var entrada = 'um <b data-remove>negrito<b>negrito interno</b>externo</b> aqui <b>negrito</b> <i>italico</i>.';
var regex = /<(b)>.*?<\/>/g;
// limpa DOM para imprimir
document.body.innerHTML = "";
entrada.replace(regex, function(match) {
console.log(match);
// para imprimir do DOM
document.body.appendChild(document.createTextNode(match));
document.body.appendChild(document.createElement("br"));
return match;
});
body {
white-space: pre;
font-family: monospace;
}
My knowledge of ER is limited, and has reached the limit in this situation. So I'm waiting for some precious tip from some expert on ER, or a "Forget it's not possible with ER = (".
Edit 2 Expected solution:
The form I look for and do not know how to do would be something that counts / accumulates the occurrences of opening tags and ignoring the closures until it is the closing pair for opening (equivalent to the first opening tag). >
If there is any doubt please comment!
Edit 1: My real case for a better understanding of the problem:
This real example is only meant to demonstrate the context where I am using the function in question, and why not be able to do this via jQuery or any other parser in the browser DOM. Well I need to leave the correct DOM so that CSS is applied correctly and only after the conversion to style inline I can remove what it was just for the Browser to render correctly and then get the result of my expected template. >
$(function() {
$('#btnGenerateHtmlMail').click(function(ev) {
var $report = $('#report');
convertCssToInlineStyle($report);
var reportHtml = $report.html();
reportHtml = reportHtml
/* remove class attribute */
.replace(/class=('|").*?/g, "")
/* remove id attribute */
.replace(/id=('|").*?/g, "")
/* remove comments html */
.replace(/<!--.*?-->/g, "")
/* remove tab, enter and whitespace */
.replace(/\s\s+/g, ' ')
// ----->>> // esse é o meu caso de problema, nesse exemplo não da problema pois nnão há tags iguais dentro do tr, mas sei que isso seria um bug que quero resolver para tornar a ferramenta generica
.replace(/<(tr) data-remove="true".*?>.*?<\/>/g, function replacer(match) {
console.log(match);
return match.match(/{{.*?}}/g);
});
$('#result').text(reportHtml);
});
});
/* Metódos irrelevantes para o problema */
function getCssDeclared($elem) {
var sheets = document.styleSheets,
o = {};
for (var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules) {
if ($elem.is(rules[r].selectorText)) {
o = $.extend(o, css2json(rules[r].style), css2json($elem.attr('style')));
}
}
}
return o;
}
function css2json(css) {
var s = {};
if (!css)
return s;
if (css instanceof CSSStyleDeclaration) {
for (var i in css) {
if ((css[i]).toLowerCase) {
s[(css[i]).toLowerCase()] = (css[css[i]]);
}
}
} else if (typeof css == "string ") {
css = css.split("; ");
for (var i in css) {
var l = css[i].split(": ");
s[l[0].toLowerCase()] = (l[1]);
}
}
return s;
}
function convertCssToInlineStyle($root) {
$root.each(function() {
var $item = $(this);
var style = getCssDeclared($item);
$item.css(style);
// recursive call chields
convertCssToInlineStyle($item.children());
});
}
table {
border-collapse: collapse;
border-spacing: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
}
table td, table th {
padding: 8px;
padding-top: 3px;
padding-bottom: 3px;
line-height: 1.428571429;
border: 1px solid #ddd;
}
table > tfoot {
font-weight: bold;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><divid="report">
<table>
<thead>
<tr data-remove="true">
<th>{{theadContent}}</th>
</tr>
</thead>
<tbody>
<tr data-remove="true">
<th>{{tbodyContent}}</th>
</tr>
</tbody>
<tfoot>
<tr data-remove="true">
<th>{{tfootContent}}</th>
</tr>
</tfoot>
</table>
</div>
<div id="tools">
<button id="btnGenerateHtmlMail">
Gerar HTML E-mail
</button>
<div contenteditable="true" id="result" style="width: 99%;resize: none;border: 1px solid #ccc;padding: 0.5%;"></div>
</div>
Note: In this example (real) not the problem because there are no equal tags inside the tr, but I know this would be a bug that I want to solve to make the tool generic.