How to combine regular expressions and template literals in JavaScript? [duplicate] - javascript

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?

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.

You just want to use String.raw
const variable = 'text'
const regexp = new RegExp(String.raw `\"mediaType\":\"${variable}\"`)
console.log(regexp)

Related

Regular expresion for RegExp in Dart

I have the following in JavaScript:
function escape(text)
{
var tx = text.replace(/[&<>"']/g);
}
Im having problems trying to do the same on Dart:
var reg = new RegExp("/[&<>"']/g"); -->this throws error.
How can I get an equivalent expression?
The Dart RegExp source does not use / to delimit regular expressions, they're just strings passed to the RegExp constructor.
It's usually recommended that you use a "raw string" because backslashes mean something in RegExps as well as in non-raw string literals, and the JavaScript RegExp /\r\n/ would be RegExp("\\r\\n") in Dart without raw strings, but RegExp(r"\r\n") with a raw string, much more readable.
In this particular case, where the string contains both ' and ", that becomes harder, but you can use a "multiline string" instead - it uses tripple quote characters as delimiters, so it can contain single quote characters unescaped (it doesn't have to actually span multiple lines).
Dart doesn't have something similar to the g flag of JavaScript regexps. Dart regexps are stateless, it's the functions using them which need to care about remembering where it matched, not the RegExp itself. So, no need for the g.
So:
RegExp(r"""[&<>"']""");
// or
RegExp(r'''[&<>"']''');
That gets a little crowded with all those quotes, and you can choose to use a non-raw string instead so you can escape the quote which matches the string (which is easier because your RegExp does not contain any backslashes itself):
RegExp("[&<>\"']");
// or
RegExp('[&<>"\']');
If you do that when your regexp uses a RegExp backslash, then you'll need to double the backslash, something which is easy to forget, which is why raw strings are recommended.
You forgot to escape double quotes
new RegExp("/[&<>\"']", 'g');

How to make \ a literal back slash using Regexp in JS

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?
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.
You just want to use String.raw
const variable = 'text'
const regexp = new RegExp(String.raw `\"mediaType\":\"${variable}\"`)
console.log(regexp)

Create regular expression dynamically with regex chars

Say I want to match any of these files:
/foo/bar/baz/x
/foo/bar/baz/y/z
so I create a regex like so:
new RegExp('^/foo/bar/baz/.*')
however my question is - how do I tell the RegExp constructor to view . * and ^ as regular expression characters, not literal characters?
As this is node.js related, my solution would be to use quotemeta
https://github.com/substack/quotemeta
as if it was perl related my solution would be to use \Q \E ;-)
new RegExp('^/foo/bar/baz/.*')
...and
/^\/foo\/bar\/baz\/.*/
...are equivalent. Everything in the new RegExp string are treated as regular expression characters, not literal characters (luckily / does not need to be escaped).
If you do want to make something a literal character using the constructor syntax, make sure you use a double backslash since \ within a string literal has its own meaning. For example, if you want to capture a literal . character, this:
new RegExp('\.')
...won't work right because that is interpreted the same as '.', making the RegExp the same as /./. You'd need to do this instead:
new RegExp('\\.')

How do I include symbols in regular expression constructors?

I want the following regular expression:
/(ending)$/
Where ending is a variable. I discovered that to use variables with regular expressions I must use regular expression constructors. So I tried:
var pattern = new RegExp((ending)$);
But this does not work either! This works if I do not include the grouping parenthesis and dollar sign, but I need those special characters as part of my pattern!
I tried to wrap the special characters in quotations, and also cancel them out by using a backslash, but nothing seems to work! What should I do to include special characters in my regular expression constructor?!
it takes a string...
var pattern = new RegExp("(" + ending+ ")$");

JavaScript split function - use of escape characters?

The following two examples do the same thing.
I was wondering why Option 1 is given in a code example I found and not Option 2?
What is the significance of the forward/backward slashes in '/\&/'
Option 1.
var pairs = qString.split(/\&/);
Option 2.
var pairs = qString.split('&');
split() is a function that can take a regex as well as a string parameter, the forward slash usage is something called a regex literal and it is not really passing a string but a regex object.
The following statements in javascript are the same.
var regex = /\&/; // Literal
var regex = new RegExp("\\&"); // Explicit
Option 1 uses a RegEx constant which is declared with surrounding forward slashed (/).
Option 2 uses a string.
See https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
The first example splits on a regular expression (constructed using the leaning-toothpick (/.../) syntax), while the second splits on a plain string.
Regular expressions are a powerful sub-language that allow complex string matching; in this case, the overhead of using one to split on a literal character (while probably negligible) is a little silly. It's like hiring a top-notch architect to build a wooden cube.
In the first example, the & character is mistakenly escaped (with the \), since it is not special in regular expressions. The regular expression engine gracefully handles that, however, and still treats it as a literal &.

Categories