-3

I'm struggle with an issue and don't understand why is this happen. I have an regex, which store in DB and for validation it's taken from server.

But on client side when I'm trying to transform my regex from string using new RegExp() I'm struggle with a problem.

My code new RegExp('\d'); returns /d/, but should /\d/, is anybody can help me with this? Where I miss the things?

I was looked this answer Javascript RegEx Not Working but I don't think that my issue is duplicate of existing one. Because of here is explained how to convert to regex from string, but in my issue new regExp eat '\' symbol and I'm not understand why and what should to do to avoid this?

BorHunter
  • 893
  • 3
  • 18
  • 44
  • This question is a duplicate of [Javascript RegEx Not Working](//stackoverflow.com/q/6521572). To understand why you need to double-up the slashes, see [this](https://stackoverflow.com/a/43774726/2025923) answer of me. In short, `\` inside string is used to escape following character. – Tushar May 29 '17 at 08:49
  • *In short, **``\``** inside string is used to escape following character.* – Wiktor Stribiżew May 29 '17 at 10:47

1 Answers1

1

Backslash () should be escaped in string literals. The following will work for you.

console.log(new RegExp('\\d'))
Gokhan Kurt
  • 8,239
  • 1
  • 27
  • 51