Why you should not use regex for parsing html / xml
Generally, it is not a good idea to use regex for anything like html or xml parsing. It is better to just write a script that in a way simulates "real parsing" (e.g. you use built-in functions to parse parts of the string) which is most of the time sufficient or use "real parsing" like going through all the characters one by one.
Regex is harder to alter and expand because it is often a very specific and tight use-case and harder to understand in general.
Besides, regex is quite poor performance-wise. If you use this code very frequently I would suggest you write a simple script to do the job. JavaScript has some indexOf and lastIndexOf methods that can help a ton.
Alternative solution
How about the following:
function matchBetween(openDelimiter, closeDelimiter, input, stripArray = []) {
const from = input.indexOf(openDelimiter);
let result = '';
if (from !== -1) {
const to = input.lastIndexOf(closeDelimiter);
if (to !== -1) {
result = input.substring(from + openDelimiter.length, to);
for (let i = 0; i < stripArray.length; i++) {
result = result.replaceAll(stripArray[i], '');
}
}
}
return result;
}
const examples = [
'<br><i>"hello olá - ok@tchau"</i><br>',
'<br><i>"another text"</i><br>',
'<br><i>"hello"</i><br><br><i>"ok"</i><br>'
];
const openDelimiter = '<br><i>';
const closeDelimiter = '</i><br>';
const stripArray = [openDelimiter, closeDelimiter];
for (let i = 0; i < examples.length; i++) {
console.log('#' + i, matchBetween(openDelimiter, closeDelimiter, examples[i], stripArray));
}
It is just a very simple example, but most of the time a function like that is already sufficient. Also you can easily extend the functionality as you go.