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+ ")$");
Related
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)
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)
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('\\.')
I have found the below Javascript recently, and (believe) I understand its operation, but cannot figure out (what appears) to be a ¿regex string class? ("/\W/.test")
function AlphaNumericStringCheck(text)
{
if (/\W/.test(text.replace(/^\s+|\s+$/g,""))) return false;
return true;
}
Can someone put a name to this technique, so I can research it more?
The /\W/ in your source code is a regular expression literal (MDC link, as MDC is about 18X clearer than the specification). Just as with a string literal ("foo"), a regular expression literal is a way of writing regular expressions in the code. The / characters in a regular expression literal are analogous to the quote characters in a string literal. In a string literal, what's inside the quotes is the content of the string; in a regular expression literal, what's inside the / characters is the regular expression. (There can also be flags following the ending /.)
So this:
var rex = /\W/;
...creates a regular expression object for the regular expression \W (match one word character). It's (essentially) equivalent to:
var rex = new RegExp("\\W");
Note that in the long form, I had to escape the backslash in the string, since backslashes are special in string literals. This is one of the reasons we have regular expression literals: Because it gets very confusing, very quickly, when you have to escape all of your backslashes (backslashes being a significant part of many regular expressions).
Regular expressions are objects, which have properties with functions attached to them (effectively, methods, although JavaScript doesn't technically have methods per se). So /\W/.test(...) calls the test function on the regular expression object defined by the literal /\W/.
\W is a shortcut (shorthand character classes or extensions) for word. Just like \d for digits and \s for whitespace and new lines. It depends on the implementation of the regex you're using.
This literals are replaced by full expression before compiled. \d turns into [0-9] and \W probably turns into [0-9a-Z][0-9a-Z]* or similar. They are designed to make your expressions more readable.
You can see some more of them here: http://www.zytrax.com/tech/web/regex.htm#special
I have a working reg exp:
var re = /([^\wåäö]*?)([\wåäö]+)([^\wåäö&]?|$)|.+/ig;
When I replace it with this one it does not work any more:
var re = new RegExp("([^\wåäö]*?)([\wåäö]+)([^\wåäö&]?|$)|.+", "ig");
Should not these two be the same?
\ is an escape character in regex as well as in a string. You need to double up your \s in the 2nd version.
You should escape the \ using \\. When you type, for instance, \w inside a regular expression literal (/\w/), it follows the regular expression syntax (which allow w after a \). When you do the same thing inside a string, it follows the string syntax, which does not allow it. So, you shoud instead write "\\w" to achieve the same effect (in other words, the RegExp's source will be \w).