const regex = /^((u|is|has)\-[a-z0-9][a-zA-Z0-9]*|([A-Z][a-zA-Z0-9]*(__[a-z0-9][a-zA-Z0-9]*)?(--[a-z0-9][a-zA-Z0-9]*)?))$/mg;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^((u|is|has)\\-[a-z0-9][a-zA-Z0-9]*|([A-Z][a-zA-Z0-9]*(__[a-z0-9][a-zA-Z0-9]*)?(--[a-z0-9][a-zA-Z0-9]*)?))$', 'mg')
const str = `is-state
is-State
is-stateActive2
u-utility
u-Utility
u-utilityClass2
has-child
has-Child
has-childClass2
his-child
a-child
1-child
A-child
js-test
Block
Block--mod
Block__el
Block__el--mod
BlockBox
BlockBox--modMod
BlockBox__elEl
BlockBox__elEl--modMod
block-box
Block-box
blockBox
5block
B__m--e
B__M--E
B__m--E
B__M--e
b__M--E
b__m--E
b__M--e
b__m--e
B__m__e
B--m--e
B--m__e
B_m-e
B-m_e
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions