Question about RegExp

Why in this code

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor

  let re = new RegExp(target + "$", "i");

  return re.test(str);
}

console.log(confirmEnding("Bastian", "n"));

Do we need the “new”. In the explanation it says because we will use a variable that will change the pattern each time the function is called.

But wont the “target” in the Regex change by itself without the use of “new”?

Also why does the code do (target + $)? Isnt the syntax like this (target$)

Thanks,

Andrej

This is so you don’t modify the original prewritten code, because the “new” literally means create a new variable that uses that function.

See, target"$" is different that target$. target$ can be a variable, but target"$" means the variable/parameter target with the syntax “$”. So it’s not the same.

1 Like

this is string concatenation, you are adding at the end of the value of target the symbol “$”

1 Like

So here

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor

  let re = new RegExp(target + "$", "i");

  return re.test(str);
}

console.log(confirmEnding("Bastian", "n"));

Target is “n” so what is $?

if target has value of "n" than target + "$" makes "n$"

1 Like

Well, Target is “n”, “$” is concated into the parameter. So it became “n$”.

1 Like

So when you call

return re.test("Bastian");

Your Regex is looking for n$? This doesnt make sense to me.

in Regex, the syntax “$” means beginning of a string or end of a string depending where you put it. So “n$” means end with “n”. So if its “$n”, it means start with “n”.

1 Like

new RegExp is creating a new regular expression, which will have inside "n$" so /n$/
in regular expressions the $ has a specific meaning


only end of string, beginning of string is a different symbol

2 Likes

Yup, I’m sorry for the confusion. “$” is only end of string. The beginning of a string is a different symbol. Thanks for correcting me.

1 Like

So if you did

What would that look for? Wouldnt it look for something that ends in “target”? Or is it a variable.

that would be a syntax error
you can’t write target $ just like that, it has no meaning in javascript

2 Likes

There’s two ways typically to regex matches:

const regex_pattern = /222$/i;
and

const variable = '222';
const regex_pattern = new RegExp(variable + '$', 'i');

Both of these are essentially the same, whereas the 2nd one allows you to dynamically change the variable.

I highly recommend using the 1st one (regex literal) vs 2nd one unless you need to dynamically change your regex.

The $ means return matches only if it matches end of the string.
The i means ignore capitalization.

Notice I did /222$/ which would match your screename because it’s andrejjj222.

1 Like

As @ILM said, That would be a syntax error. You cannot put in spaces in variables or parameter unless it is inside a string. So as @ILM already said again, target $ will spit out Syntax Error.

Ok, but when you are writing regex

re = RegExp(/\hey$/

Doesnt it work like that?

Didnt see post above before posting this

in that case you would write a regular expression literal, which you can totally do if you don’t need to change anything, like re = /n$/i;
but in the code you posted you are creating a regular expression from a variable, and in that case you need to use string methods and operators

Normally, when we use Regex, we use the flags / /. You can put your regex inside the slashes, so this exercise would essentially equals to

let re = /n$/;
//This is called Literal Notation

It’s just trying to teach you the original formula.

Ok thank you, dont you have to put \ before 222?

1 Like

The syntax \ means escaping, that means the code will see it as it’s own thing. for example “$” in regex means end of string, but if you do \$, it will be escaped into a simple “$” string.

1 Like

if you use new RegExp it will be that that will put the / in the regular expression
if you do
let re = new RegExp(<a>, <b>);
it creates a regular expression like /<a>/<b>

Please note that you still need to consider the general escaping rules for strings, for example if you want a backslash in the string you need to escape it and so write \\

1 Like