Open mikezimm opened 1 day ago
The problem was that in the code, I was replacing double quotes character, not single quotes.
I tried this which should have worked... just added the ' to the regex.
However, it did not work. I had to add it and then escape it as well. When I did that, typescript told me it was unnecessary... however it seems it is necessary to escape it that way.
Explanation of the Unnecessary Escape Warning: In a regular expression, you don't actually need to escape a single quote ('). The warning you're seeing is due to TypeScript or your editor trying to optimize your code and point out that the escape sequence for the single quote isn't required.
The warning is coming from TypeScript (or your editor) flagging the unnecessary escape character. In a regular expression, [' inside [...] is fine as-is, but when you escape it (\'), the editor might think you're trying to escape it for some special reason (like for the string delimiters outside the regex).
However, as you found out, escaping the single quote isn't required for the regex to work, but it doesn't break anything either. It may have just removed ambiguity in how the code was parsed.
Copy code
function sanitizeFilename(filename) {
// Regex for invalid SharePoint characters, including single quote
return filename.replace(/[\\/:*?"<>|#&']/g, '');
}
// Example usage:
const sanitizedFilename = sanitizeFilename("Invalid'File/Name?*#&'Test.txt");
console.log(sanitizedFilename); // Output: "InvalidFileNameTest.txt"
The warning about the unnecessary escape character is just TypeScript trying to optimize your code. It's safe to ignore, as the code works correctly either with or without escaping the single quote. The key takeaway is that in the context of a character class in regular expressions, you don't need to escape the single quote, and removing the escape will make the code cleaner and more maintainable.
It seemed to create the list item but not the file name