Skip to content

Instantly share code, notes, and snippets.

@antic183
Last active March 13, 2024 08:45
Show Gist options
  • Save antic183/619f42b559b78028d1fe9e7ae8a1352d to your computer and use it in GitHub Desktop.
Save antic183/619f42b559b78028d1fe9e7ae8a1352d to your computer and use it in GitHub Desktop.
remove utf-8 bom with javascript
// remove utf-8 BOM from ressource "res"
// res.charCodeAt(0) === 0xFEFF | res.charCodeAt(0) === 65279
if (res.charCodeAt(0) === 0xFEFF) {
res = res.substr(1);
}
@matthewmb
Copy link

Thank you. This was the final solution to hours of frustration.

@dharb
Copy link

dharb commented Jan 15, 2020

Seconded. What a nightmare, thank you so much for sharing this.

Copy link

ghost commented Dec 3, 2020

one-liner alternative

res = res.replace(/^\uFEFF/gm, "");

or better yet:

res = res.replace(/^\uFEFF/gm, "").replace(/^\u00BB\u00BF/gm,"");

@Darker
Copy link

Darker commented May 12, 2021

What is the second one for?

@brdacost
Copy link

up

@cescoallegrini
Copy link

cescoallegrini commented Oct 9, 2022

What is the second one for?

@Darker those are the BOM representation in excel files (»¿), or at least part of it.
As far as I know the full output is , so the regex should be

res = res.replace(/^\uFEFF/g, "").replace(/^\u00EF?\u00BB\u00BF/g,"");

@vassudanagunta
Copy link

one-liner alternative

res = res.replace(/^\uFEFF/gm, "");

A BOM only exists at the beginning of the file. This regex replaces it at the beginning of every line. Unlikely to matter, but it is incorrect.

@michabaur
Copy link

A BOM only exists at the beginning of the file. This regex replaces it at the beginning of every line. Unlikely to matter, but it is incorrect.

Omitting both the g and m flags would solve this, I guess: res = res.replace(/^\uFEFF/, "");

@antic183
Copy link
Author

antic183 commented Aug 17, 2023

@michabaur yes that's true. Why don't you just use my version. Substring is faster than regular expressions. One line more or less doesn't change anything. In development there are many empty heads who always think they know something better. I don't mean you, but you don't have to look far.

@cescoallegrini Editing office documents with js is a pain in the ass.

@eladkarako I think you dont know what you do.

@michabaur
Copy link

@antic183 I agree. In my deployment tool chain it lends itself to doing this in an ant replaceregexp task. So I go the regex way ;-)

@Grammostola
Copy link

Thanks a whole lot for this. Can be a bit mystifying when you first encounter a problem caused by this char. Excellent solution.

@ianthedev
Copy link

@michabaur yes that's true. Why don't you just use my version. Substring is faster than regular expressions. One line more or less doesn't change anything. In development there are many empty heads who always think they know something better. I don't mean you, but you don't have to look far.

@cescoallegrini Editing office documents with js is a pain in the ass.

@eladkarako I think you dont know what you do.

One has nothing to lose to be friendly or to not be unfriendly.

@antic183
Copy link
Author

antic183 commented Mar 13, 2024

@ianthedev
I could have phrased the comment to "cescoallegrini" and "eladkarako" more nicely, I agree with you. But it is simply a fact that many developers always think they know something better and this is often unjustified. Whoever gives it out must also be able to take it. And there are also many developers who sell themselves beyond their worth. Unfortunately, I have experienced this too often. In my opinion, the worst thing about it is that false information is shared and beginners learn the wrong thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment