Reputation: 624
I wish to write something like:
var N = document.querySelectorAll("input[id^='left"+/^[0-9]*$/+"right']").length;
How do I do in JS / JQuery?
Upvotes: 0
Views: 3081
Reputation:
You can't. Attribute selectors do not support regexp. You can select based on prefix and suffix, though:
[id^="left"][id$="right"]
If you want to exclude elements with IDs missing the digits in between left
and right
, you'll have to filter them out:
Array.from(queryResults).filter(elt => /^left\d*right$/.test(elt.id));
In general, however, using IDs with complex internal structure is often not the best approach. You will have to keep constructing them and picking them apart. Consider using classes and/or data attributes, or it's often the case that if you are creating the elements yourself, you can just remember the elements and reference them directly.
Upvotes: 3