javascript - Regex to escape HTML Tag -
i want extract html tag string name only. obtain result:
- farm 1
- staging
- staging_system_10
what type regular expression can use?
<div class='singlenode'><i class='fa fa-cogs'></i><span>farm 1<span class='badge badge-primary'></span><span></div> <div class='singlenode'><i class='fa fa-cubes'></i><span>staging<span class='badge badge-primary'></span><span></div> <div class='singlenode'><i class='fa fa-desktop'></i><span>staging_system_10<span class='badge badge-primary'></span><span></div>
in case have use regex, here sample code:
var re = /<div[^>]*?>(?:<(\s+)[^>]*?>[^<]*?<\/\1>)+<span[^]*?>([^<]*?)(?=<span )/g; var str = '<div class=\'singlenode\'><i class=\'fa fa-cogs\'></i><span>farm 1<span class=\'badge badge-primary\'></span><span></div>\n\n <div class=\'singlenode\'><i class=\'fa fa-cubes\'></i><span>staging<span class=\'badge badge-primary\'></span><span></div>\n\n<div class=\'singlenode\'><i class=\'fa fa-desktop\'></i><span>staging_system_10<span class=\'badge badge-primary\'></span><span></div>'; var m; while ((m = re.exec(str)) !== null) { if (m.index === re.lastindex) { re.lastindex++; } // view result using m-variable. // m[2] 2nd capture group, text inside div element alert(m[2]) }
in case can parse dom, use code:
var input = document.getelementsbytagname("div"); ($i = 0; $i < input.length; $i++) { alert(input[$i].textcontent); }
<body> <div class='singlenode'><i class='fa fa-cogs'></i><span>farm 1<span class='badge badge-primary'></span><span></div> <div class='singlenode'><i class='fa fa-cubes'></i><span>staging<span class='badge badge-primary'></span><span></div> <div class='singlenode'><i class='fa fa-desktop'></i><span>staging_system_10<span class='badge badge-primary'></span><span></div> </body>
Comments
Post a Comment