JSFiddle - React, Tailwind, and code Playground
HTML
<div id="output">
</div>
CSS
#output {
font-family: monospace;
}
JavaScript
var test_cases = [
'(1)',
'((1))',
'(((1)))',
'(1) (2)',
'(1) ((2))',
'((1) (2))',
'(1)(2)',
'(1)((2))',
'((1)(2))',
];
function matchValidTags(s) {
if( s.match(/\(\(\d+\)\)/) ) return null;
return s.match(/\(\d+\)/);
}
function process(s) {
var m = s.match( /\(+.+?\)+/g );
$('#output').html(
$('#output').html() + '<br/>' +
m.join( ', ' ) + ' --> ' +
m.filter(matchValidTags).join( ', ' )
);
}
test_cases.forEach(process);