-2

I try to use this Regex

new RegExp('utm_source=Weekly\SRecommended\SJobs\SEmail', 'ig');

When I try to use it in regex101 or regexr it works.

And in my code doesn't work.

I try to use this in console this is the result is

/utm_source=WeeklysRecommendedsJobssEmail/gi

the code without spaces.

When I try to use space letter it works.

Any help?

Mostafa Nawara
  • 782
  • 9
  • 21

1 Answers1

0

Because \ is an escape character for regular expressions and strings. You have to escape the \ if you're creating a regex from a string:

new RegExp('utm_source=Weekly\\SRecommended\\SJobs\\SEmail', 'ig');

Or simply use a regex literal which exists precisely to avoid this problem:

/utm_source=Weekly\SRecommended\SJobs\SEmail/ig
Joey
  • 344,408
  • 85
  • 689
  • 683