0

When I am building the angular UI library using ng-packagr I am having the below regex defined in the file


> /^(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+
<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-
f]{2})*")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<
>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f
]{2})*"))*(?:,(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<
>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f
]{2})*")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>
#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]
{2})*"))*)*$/.test("CN=abcd,CN=abcd,O=abcd,C=us")
true

In order to build library I have to convert regex type to string type but I am facing issue while building the angular library.

So I tried escaping regex into a string. but regex is not in readable format. as we have to convert all '' to '\' and '.' to '\'

So I have come up with another solution using the String.raw method where I can simply pass an existing regex to String.raw method without modification

static BASEDN: RegExp = new RegExp(String.raw`^(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"))*(?:,(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"))*)*$`);

But unable to create a UI library with ngc compiler. As the AOT compiler does not support tagged template expressions; avoid them in metadata expressions. https://angular.io/guide/aot-metadata-errors#tagged-template-expressions-not-supported

Facing the below error:

regex.enums.ts:23:10: Metadata collected contains an error that will be reported at runtime: Reference to a non-exported function.
  {"__symbolic":"error","message":"Reference to a non-exported function","line":22,"character":9,"context":{"name":"RegExp"}}

So in order to solve this problem, I have got a solution on the internet as we can use // @dynamic annotation to class/function to avoid the compilation

But The @dynamic annotation is obsolete as of Ivy given that its metadata system is being phased out. So I am not sure how to solve this issue. Please help, can I use dynamic annotation here Or is there any other way to solve this problem?

Prashant Biradar
  • 301
  • 5
  • 14
  • The RegExp constructor takes a **string** parameter. You're passing in a regular expression literal. You don't need the constructor when you have a literal regular expression. – Pointy Oct 31 '21 at 13:28
  • Thanks for your inputs. Edited my question. My file contains the string, not a literal regular expression which I am passing to RegExp constructor when I use String.raw method it works like a charm. But when i build it it fail with meta data errors. – Prashant Biradar Oct 31 '21 at 13:43
  • 1
    Try one of these valid JS strings [// delims](https://regex101.com/r/UV7gKV/1) or ['' delims](https://regex101.com/r/KfF3yJ/1)or ["" delims](https://regex101.com/r/NLbRDi/1) – sln Nov 01 '21 at 18:49

0 Answers0