-2

VSCode always autofixs the Regexp string:

new RegExp("^[0-9\.]+$", "i")

to

new RegExp("^[0-9.]+$", "i")

Does anybody know how to resolve this problem?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Zhaiduo
  • 49
  • 1
  • 10
  • 2
    [Escaping `.` inside square brackets is unnecessary.](https://stackoverflow.com/questions/19976018/does-a-dot-have-to-be-escaped-in-a-character-class-square-brackets-of-a-regula) Anyway, *when* does this autofix happen? When you save? Or...? – juzraai Jul 20 '19 at 09:57
  • well, it seems work, dude~ @juzraai – Zhaiduo Jul 20 '19 at 10:06

1 Answers1

0

That's perfectly fine - if you want to match a number or a literal dot .. If you want to match a number, a literal dot, and a literal backslash, you need to escape the backslash:

new RegExp("^[0-9.\\]+$", "i")

You could use String.raw:

new RegExp(String.raw`^[0-9.\\]+$`, "i")
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • @Zhaiduo Why? It unnecessarily converts the string into a substitution free template string to then use a special function to get the "raw" string. The real solution is to escape the backslash (\\\) which Jack has "slipped" unmentioned into its answer. tl;dr: escape the backslash, remove the template stuff – Andreas Jul 20 '19 at 10:15
  • 1
    I still don't get why you're using a template string and `String.raw` o.O – Andreas Jul 20 '19 at 10:22
  • @Andreas My intention is keep the old format of RegExp string, and let VScode ignore the autofixing. Maybe it's not for others, but best for me. – Zhaiduo Jul 20 '19 at 10:23
  • I've seen it used like that @Andreas, and I'm keeping it for the sake of the original content of the answer - unless you disapprove. – Jack Bashford Jul 20 '19 at 10:24