what’s happening:
I try to use regular expression to solve the truncate a string assignment, but there seems to have something wrong with the regular expression.
My code so far
function truncateString(str, num) {
//let regex = /(?=^.{num}).+/, while num is the given parameter:
let regex = new RegExp("/(?=^.{" + num + "}).+/");
return str.replace(regex, "...");
}
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));
The return is always the whole str without any “…” at the end.
I tried to examine it by replacing the num variable into number 8(which is in the example), the return in the console shows “…”, seems like the regexp didn’t match the expected substring.
let regex = /(?=^.{8}.+)/;
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string