0

I've got a little problem of comprehension with my regex I just want add a variable inside my regex

I've got this:

static test(Prefix, version: string) {
    return "/^" + Prefix + "[0-9]*.[0-9]*.[0-9]*-[0-9]*-(SNAPSHOT)$/.test(version);
} 
// I don't understand where to put my last " 

thanks for your help

zefzef aa
  • 45
  • 4
  • What example strings would you like this regex to match? – Zdeněk Jelínek Dec 14 '20 at 11:29
  • return "/^" + Prefix + "[0-9]*.[0-9]*.[0-9]*-[0-9]*-(SNAPSHOT)$/.test(" + version + ")"; you mean something like this? I don't get the question exactly – true_gler Dec 14 '20 at 11:31
  • for example: base-1.0.0-1-SNAPSHOT – zefzef aa Dec 14 '20 at 11:32
  • 1
    Dropping unknown strings in the middle of a `regex` can produce unexpected or even invalid regular expressions. For example, if `Prefix` is `'.'` (you want the match the strings that start with a dot), the `regex` generated this way starts with `^.[0-9]*...` and matches strings no matter how they start (because in `regex` a dot matches any character). Similar for other characters that are special in `regex`. – axiac Dec 14 '20 at 11:33
  • `return new RegExp("^" + Prefix + "[0-9]*.[0-9]*.[0-9]*-[0-9]*-(SNAPSHOT)$").test(version);` – Tomasz Gawel Dec 14 '20 at 11:41

1 Answers1

1

You can build a regexp expression via new RegExp(string)

Alexey Victorov
  • 242
  • 1
  • 9