Search code examples
angulartypescriptaot

angular7 aot: Expression form not supported


In Angular7 I would like to create a util class like this:

export class FieldNameUtils {

  static toHump(name: string) {
    return name.replace(/_(\w)/g, function (all, letter) {
      return letter.toUpperCase();
    });
  }

  static toLine(name: string) {
    return name.replace('/([A-Z])/g', '_$1').toLowerCase();
  }
}

It's working well with development mode, but when I build with production, I got error like this:

field.name.utils.ts:4:25: Metadata collected contains an error that will be reported at runtime: Expression form not supported.
  {"__symbolic":"error","message":"Expression form not supported","line":3,"character":24}

I have tried to change the /_(\w)/g to a static variable, but still got error, I know lambda expression is not supported, so I'v already change (all, letter) => ... to a function(all, letter){ ... } The error shows that an expression after replace( is not supported, but it is a RegExp


Solution

  • Have you tried setting up your regex as follows?

    const regex = new RegExp('_(\w)', 'g');