2

I am pretty new to Regexp and it seems that the \ is used for meta characters. My problem is I want to search this string exactly \"mediaType\":\"img\"

Now I also want to dynamically put a variable in for img. So I want it to be something like this

new RegExp(`\"mediaType\":\"${variable}\"`)

How do I write this to make it work?

Taylor Austin
  • 5,407
  • 15
  • 58
  • 103
  • 1
    Use: `new Regex('\\\\"mediaType\\\\":\\\\"img\\\\"')` – anubhava Oct 18 '17 at 14:05
  • 1
    `\ ` is an escape character in both javascript strings and regular expression. You have to escape your escape. Also, you don't need to escape `"` in regex (if that's what you're trying to do – ctwheels Oct 18 '17 at 14:05
  • @ctwheels I need the `"` as it is part of the string I am searching for – Taylor Austin Oct 18 '17 at 14:12
  • @anubhava it was only three \\\ if I did four it gave me two \\ and I only needed one, thank you for the help! – Taylor Austin Oct 18 '17 at 14:16
  • `RegExp` instead of `Regex` – Maxim Oct 18 '17 at 14:17
  • 1
    @TaylorAustin then you should simply use `"mediaType":"${variable}"`, but honestly, I would use `"mediaType"\s*:\s*"{variable}"` instead as it takes into account the possibility of whitespace separating the two values (assuming this is possible and intended). Also, you should use the `.escape` function (click the link that your question has been marked a duplicate of). There's no need to escape the `"` since it has no special meaning in regex. If you need to escape it in javascript, however, you can do that, although I would simply use `'` instead – ctwheels Oct 18 '17 at 14:17
  • I think you misunderstood , I need the literal string \"mediaType\":\"img\", with those slashes in the string. – Taylor Austin Oct 18 '17 at 15:26

2 Answers2

2

You just want to use String.raw

const variable = 'text'
const regexp = new RegExp(String.raw `\"mediaType\":\"${variable}\"`)
console.log(regexp)
Konrad
  • 21,590
  • 4
  • 28
  • 64
1

Short answer:

function escapeRegEx(s) {
    return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

var expression = new RegExp('\\\\"mediaType\\\\":\\\\"' + escapeRegEx(variable) + '\\\\"');
// or, using a template literal:
var expression = new RegExp(`\\\\"mediaType\\\\":\\\\"${escapeRegEx(variable)}\\\\"`);

Long answer:

Besides being used for meta characters, backslash in regular expressions can be used to escape characters that would otherwise have meaning (like *, $, parentheses, and \). So the way to match a backslash in a regular expression is to add another one as an escape character: \\.

Taking that into account, the regular expression you want to end up with is \\"mediaType\\":\\"img\\", and if you were using a regular expression literal that would be it. Unfortunately it gets a little more involved because you need to create an expression dynamically, you need to provide the expression as a string, which also needs the backslashes escaped. That adds a second layer of escaping, so you need to double up each of the \ characters again, and you end up with new RegExp('\\\\"mediaType\\\\":\\\\"img\\\\"').

Another complication is that you want the contents of variable to be matched literally, not interpreted as a regular expression. Unfortunately, there's no built-in way to automatically escape regular expressions in JavaScript, so you'll need to use one of the solutions in Is there a RegExp.escape function in Javascript?. I used a slightly modified version of the accepted answer that defines it as a standalone function instead of adding it to the RegExp object. The exact solution doesn't matter, as long as you escape the dynamic part.

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
  • 1
    Alternatively keep the template literal: ``new RegExp(`\\\\"mediaType\\\\":\\\\"${ escapeRegex( variable ) }\\\\"`)`` to avoid confusion over `'`/`"` – Bergi Oct 18 '17 at 18:18
  • 1
    @Bergi That's a good point, I used concatenation out of habit, but they did use a template literal in the question. I'll add that as an alternative. – Matthew Crumley Oct 18 '17 at 18:21