const regex = new RegExp('modelBuilder\\.Entity<(?\\'type\\'\\w+)>\\(builder =>\\n\\s*\\{(?\\'builder\\'[\\w\\W]+?)\\}\\);', 'gm')
const str = `modelBuilder.Entity<Foobar>(builder =>
{
builder.HasKey(e => e.Id).HasName("PRIMARY");
builder
.ToTable("foobar")
.UseCollation("utf8mb4_0900_ai_ci");
builder.HasIndex(e => e.A, "A").IsUnique();
builder.Property(e => e.D).HasColumnType("datetime");
builder.Property(e => e.E)
.HasMaxLength(255)
.UseCollation("utf8mb4_unicode_ci");
builder.Property(e => e.N)
.HasMaxLength(255)
.UseCollation("utf8mb4_unicode_ci");
builder.HasOne(d => d.ANav).WithOne(p => p.BNav)
.HasForeignKey<Foobar>(d => d.Leerling)
.HasConstraintName("Foobar_fk");
});`;
const subst = `public class $1Mapping : IEntityTypeConfiguration<$1>\n{\n public void Configure(EntityTypeBuilder<$1> builder)\n {\n$2\n}}`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
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