regex case insensitivity

Viewed 21071

How would I make the following case insensitive?

if ($(this).attr("href").match(/\.exe$/))
{
// do something
}
5 Answers

Unlike the match() function, the test() function returns true or false and is generally preferred when simply testing if a RegEx matches. The /i modifier for case insensitive matching works with both functions.

Example using test() with /i:

const link = $('a').first();

if (/\.exe$/i.test(link.attr('href')))
   $('output').text('The link is evil.');


evil link

Fiddle with the code:
https://jsfiddle.net/71tg4dkw

Note: Be aware of evil links that hide their file extension, like: https://example.com/evil.exe?x=5

Documentation for test():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Related