Benbob
Benbob

Reputation: 14254

The regular expression doesn't work

Trying to replace any non alpha-numeric characters with a hyphen. Can't see why it shouldn't be working. It returns the original string unchanged.

item.mimetype = "image/png";

var mimetype = item.mimetype.toLowerCase().replace("/[^a-z0-9]/g",'-');

Upvotes: 0

Views: 219

Answers (2)

Kenan Banks
Kenan Banks

Reputation: 211992

Remove the quotes around the regex.

As written, Javascript is looking for the string "/[^a-z0-9]/g"

// This works
"image/png".toLowerCase().replace(/[^a-z0-9]/g,'-'); 

// And if writing unquoted regular expressions makes you feel icky:
"image/png".toLowerCase().replace(new RegExp("[^a-z0-9]", "g"), '-');

// And if I might do a full rewrite:
"image/png".toLowerCase().replace(/\W/g, '-');

More here

Upvotes: 6

jtbandes
jtbandes

Reputation: 118681

You've put a string instead of a regex. Do this:

.replace(/[^a-z0-9]/g,'-');

Upvotes: 4

Related Questions