If you are trying to match an exact string, you don't even need a regex for that:
let string = 'aside-canvas other others';
console.log(string.includes('aside-canvas'))
If you want to make sure that different class names in the list are treated separately:
let string = 'aside-canvas other others';
console.log(string.split(' ').includes('aside'))
let string2 = 'aside-canvas other others aside';
console.log(string2.split(' ').includes('aside'))
Or if you are really married to the regex idea:
(?:^| )aside(?:$| )
will check if it's either the start or has a space (to keep boundaries between classes consistent). Then the class name of interest aside
. Then if it's the end of the string or a space to make sure that's the end of the class name.
let string = 'aside-canvas other others';
let regex = /(?:^| )aside(?:$| )/
console.log(regex.test(string))
let string2 = 'aside-canvas other others aside';
console.log(regex.test(string2))
let word = 'anotherClassName';
let dynamicRegex = new RegExp(`(?:^| )${word}(?:^| )`);
console.log(dynamicRegex);
If you want to make the regex dynamically, just create a new instance of RegExp:
Just note that whatever is in word will be treated as regex rather than text. So if you want to match * for instance, you need to escape it out.
let word = 'anotherClassName';
let dynamicRegex = new RegExp(`(?:^| )${word}(?:^| )`);