I been looking for a while on google and on here and I can't find a post that is very helpful so I have this script that gets each class name id value and put it in a variable call strtext so later I use regex
to output the value of the var output but I'm noticing it is only showing the var output result as one match which is showing just
a
so how can I get the regex section to show the rest of the matches. I'm referring to this
document.querySelector('#output-2').innerHTML= output;
Here is my code
var names= document.querySelectorAll('.name');
var text = new Array();
var strtext = '';
for (var i = 0; i < names.length; i++){
var arlength = text.length;
text[arlength] = '['+names[i].getAttribute('id')+']';
}
strtext = text.join(' and ');
document.querySelector('#output-1').innerHTML= strtext;
//Show all the regex matches
var string= strtext;
var pattern= /\[(.*?)\]/ig;
var match = pattern.exec(string);
var output= match[1];
document.querySelector('#output-2').innerHTML= output;
<p id='a' class='name'>Adam</p>
<p id='b' class='name'>Bob</p>
<p id='c' class='name'>Cane</p>
<p id='d' class='name'>Dan</p>
<p id='e' class='name'>Ed</p>
<p id='f' class='name'>Fred</p>
<p id='g' class='name'>Gene</p>
<p id='output-1'></p>
<p id='output-2'></p>