Basic Algorithm Scripting: Confirm the Ending + regex

Basic Algorithm Scripting: Confirm the Ending + regex
0

#1

Hi Campers!

Regarding this task

I wonder is it possible to solve it using RegEx?

First I tried this way:

function confirmEnding(str, target) {
  let regex = new RegExp(target);
  return regex.test(str);
}

And It almost worked … Why almost? Because I forgot about $.
When I changed to :

function confirmEnding(str, target) {
  let regex = new RegExp(target$);
  return regex.test(str);
} 

It crashed… Can anyone tell me, is that possible to solve that task via RegEx?

Thank You!


#2

Why did you put $ on the end of the variable named target?

Yes, it is possible to use a regular expression to solve this challenge. See if you can figure out how.


#3

Thank You! : )
Your hint helped…
Is there more “beautiful” way for that, than mine?

Spoiler
function confirmEnding(str, target) {
  let regexEnd = target + '$';
  let regex = new RegExp(regexEnd);
  return regex.test(str);
}

#4

You could skip the extra variable if you wanted.

let regex = new RegExp(target + '$');

OR

let regex = new RegExp(`${target}$`); // uses a String Literal

#5

Perfect!
Thanks a lot!


#6

Hi

please could you explain a little about this RegExp solution. The RegExp constructor function was not covered in the Regex lessons and I am a bit confused. Why is it written here as

RegExp(target + '$')

?


#7

Take a look at RegExp documentation first and see if this clears up your confusion. If not, then let me us know what you do not understand about the code or the documentation.


#8

Hi Bro!!
I am new here !


#9

I am just confused of what I should learn
Java or Kotlin
??
:sweat_smile::sweat_smile::sweat_smile::sweat_smile:


#10

Hi

thank you for your reply Randell. I looked at both the MDN guide and the reference, but could not find any information on the particular syntax you used. From the MDN reference I found:

"There are 2 ways to create a RegExp object: a literal notation and a constructor. To indicate strings, the parameters to the literal notation do not use quotation marks while the parameters to the constructor function do use quotation marks. So the following expressions create the same regular expression:

/ab+c/i;
new RegExp('ab+c', 'i');
new RegExp(/ab+c/, 'i');

I am confused about why you have used a + sign, and why is the ‘$’ in quotes.


#11

Using new RegExp to create a regular expression, allows you to create the expression dynamically, so you can use variables. I will construct a regular expression first using literal notation and then I will use a constructor (like what you are asking about) to create the same regular expression.

Let’s say that I have the following regular expression (using literal notation). This will match try and match the letters abc or ABC in a string.

var exp = /abc/i;

Below, I use the the test method with a string and the above regular expression.

var exp = /abc/i;
var str = "It is as easy as ABC, and easy as 123."
console.log(exp.test(str)); // displays true to the console.

Now, I will use a constructor to create the same regular expression.

var lettersToFind = "abc";
var exp = new RegExp(lettersToFind, i);
console.log(exp.test(str)); // displays true to the console.

See how I used the lettersToFind variable to build the regular expression?

So how does this relate to RegExp(target + '$')I suggested in this challenge? Well, I know I want to check if target (a variable containing a specific string) is located at the end of another string. I can not use a regular expression using literal notation, because I don’t know what the value of target is to write the expression literally. I do know if I have a regular expression with $ at the end of it, that means what ever is before the $, must appear at the end of the string in order to be considered a match. So, I need to create a string to use in the RegExp constructor which takes the value of target and concatenates the $ to it. So if target was “cheese”, target + ‘$’ would be ‘cheese$’, which would match strings which end with ‘cheese’.

Hopefully, this better explains why I used the syntax I did.


#12

Thank you so much Randell - this explains it perfectly for me. This syntax with the + sign is concatenating to achieve the desired regex query, which seems blindingly obvious to me now! I can see how powerful this technique is.

I am much happier now that I understand the purpose and meaning of the ‘new Regexp constructor method’.

Thank you for your patience :slight_smile:


#13

Hey Randall,
Read through some of the documentation, but I’m still not clear on the difference between these two and was hoping you could help explain it in laymen’s terms?

//where target is an argument passed into the function.
let targetReg = new RegExp (target + '$');  //works
let targetReg = /target$/;         //Doesn't work

When I switch these out, one works and one doesn’t and I simply don’t know why. Thanks!


#14

The first uses constructor notation, where target is a variable. The second uses literal notation, so target is just the characters t, a, r, g, e, t.

With constructor notation, I could write:

let target = 'able';
let targetReg = new RegExp (target + '$');  //works
const isMatch = targetReg.test('table'); // isMatch is true because 'table' ends with 'able'