Reputation: 1
Could you please provide regular expression for identifying all divs in javascript whose id begins with com
e.g <div id="_com_1">
Thanks for your help
Upvotes: 0
Views: 92
Reputation: 6334
var divs=document.getElementsByTagName('div');
for(var i=0;i<divs.length;i++){
if(/^_com/.test(divs[i].id)){
//do something
}
}
This assumes the divs are already a part of the DOM.
Upvotes: 1
Reputation: 42026
If you have jQuery on the page, you can use $('div[id^="_com_"]')
which is by the way a valid CSS3 selector: div[id^="_com_"]
.
I would recommend using that instead of regexing through the entire HTML, which might find false positives in text or html comments.
Upvotes: 2
Reputation: 78520
/^_com/.test("_com_1")
<-- evaluates to true
would work for this situation, but for simply beginning with com you need
/^com/.test("_com_1")
<-- evaluates to false
Upvotes: 0