Hello everyone ! I’ve been struggling recently with a code I came up with to check if a character in a string has several occurence :
function hasUniqueChars(str){
str = str.toLowerCase();
for (i=0;i<str.length;i++) {
var re = new RegExp(str[i] , "gi");
if (str.match(re).length > 1) {
return true
}
}
return false
}
It has worked so far, but the problem is that if the string contains a special characters, aka [+ - / ? ! etc … ] the code will crash since you cannot use those characters as a string inside a regexp, you rather have to use a backslash to escape the expression.
So, does anyone know a way to pass a special character inside a variable and then inside a regexp? That would be of a great help. Thank you !