Reputation: 31815
I'm using javascript to test for the ending of a file that is selected to be uploaded to the server.
The regex is this:
(jpg|jpeg|png|gif|bmp)$
And it works fine as long as the file extensions are in lower case, but when I do this
/(jpg|jpeg|png|gif|bmp)$/i
I doesn't match anything.
Can someone tell me why? What am I doing that is wrong?
Upvotes: 0
Views: 119
Reputation: 1331
this works..
var reg=/(jpg|jpeg|png|gif|bmp)$/i
document.write(reg.test('bMp'))
Upvotes: 0
Reputation: 20932
Here's sample code that works, using both regexp syntaxes:
var rxp = new RegExp('(jpg|jpeg|png|gif|bmp)$', 'i') var rxp2 = /(jpg|jpeg|png|gif|bmp)$/i document.write( rxp.exec('foo.jpg') + "<br/>" ) document.write( rxp.exec('foo.JPG') + "<br/>" ) document.write( rxp2.exec('foo.jpg') + "<br/>" ) document.write( rxp2.exec('foo.JPG') + "<br/>" )
Upvotes: 0
Reputation: 519
use 'i' as parameter i.e. RegExp("(jpg|jpeg|png|gif|bmp)$", "i")
Upvotes: 2
Reputation: 269648
/(jpg|jpeg|png|gif|bmp)$/i
You should probably include the .
in there too, so that "foo.dfpng", "bar.oejpg" etc don't count as valid:
/\.(jpg|jpeg|png|gif|bmp)$/i
Upvotes: 2
Reputation: 31913
Use forward slashes as the boundaries of your regex, not backslashes.
/(jpg|jpeg|png|gif|bmp)$/i
Upvotes: 1
Reputation: 14648
the slashes are wrong. Reverse them (need forward slash). Backward slashes are used for escaping characters.
Upvotes: 0