The variable htmlStr may contain different spellings of id:
var htmlStr = "<div id="demo_div"></div>";
var htmlStr = "<div id='demo_div'></div>";
var htmlStr = "<div id=demo_div class="demo"></div>";
var htmlStr = "<div id=demo_div></div>";
How can I write this differently without many try-catch functions? Can I combine the patterns? It works - but does not look pretty.
var idname;
try {
idname = /(id="(.*?)(\"))/g.exec(htmlStr)[2]
} catch (e) {
try {
idname = /(id='(.*?)(\'))/g.exec(htmlStr)[2]
} catch (e) {
try {
idname = /(id=(.*?)(\ ))/g.exec(htmlStr)[2]
} catch (e) {
try {
idname = /(id=(.*?)(\>))/g.exec(htmlStr)[2]
} catch (e) {
console.log(e);
}
}
}
}
console.log(idname);