tags:

views:

61

answers:

2

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",'-');
+4  A: 

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

.replace(/[^a-z0-9]/g,'-');
jtbandes
+6  A: 

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

Triptych