const regex = /class [A-Za-z_$]*\s*\{/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('class [A-Za-z_$]*\\s*\\{', 'gm')
const str = `class Entity
{
constructor(game,imageURL)
{
this.game = game;
this.sprite = new Sprite(this.game.c,imageURL);
}
draw(depth,offset,textureSize,textureCoordinates,offsetX=0)
{
let scaleFactors = {
1:1,
2:0.65,
3:0.45
};
let scaleFactor = scaleFactors[depth];
let x = Math.floor(offset.x + textureCoordinates[0] + (textureSize[0] / 2) - (this.sprite.image.width * scaleFactor / 2));
let y = Math.floor(offset.y + textureCoordinates[1] + (textureSize[1] / 2) - (this.sprite.image.height * scaleFactor) + 6 * scaleFactor);
x = x + offsetX * scaleFactor;
let width = Math.floor(this.sprite.image.width * scaleFactor);
let height = Math.floor(this.sprite.image.height * scaleFactor);
let entityFades = {
1:1,
2:0.5,
3:0.1
}
let filter = '';
if(this.game.level.entities.fadeType === 'darken')
{
filter += 'brightness('+entityFades[depth]+')';
}
this.sprite.draw(
x,
y,
width,
height,
0,
0,
this.sprite.image.width,
this.sprite.image.height,
filter
);
}
}`;
// 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