fps-solutions / FPS-Photo-Form

MIT License
0 stars 0 forks source link

Unable to create item with an 'apostrophe' in the title #81

Open mikezimm opened 1 day ago

mikezimm commented 1 day ago

It seemed to create the list item but not the file name

mikezimm commented 11 hours ago

Same as https://github.com/fps-solutions/FPS-Photo-Form/issues/9

The problem was that in the code, I was replacing double quotes character, not single quotes.

mikezimm commented 11 hours ago

Update on code...

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.

Here's the ChatGPT version of explanation:

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.

However, let's clarify why escaping the single quote worked:

  1. Single Quote in a Character Class:
  1. Why the Escape Might Have Worked:

Why the Warning Was There:

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.

Best Practice:

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"

Conclusion:

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.