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
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.
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’.
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!