If you have used regular expressions in JavaScript before you probably know that you can match any non-line-terminating character with just the dot (/./
). Of course, you may also want to match absolutely any character in which case you can use any of the following:
-
Most Common:
/[\S\s]/
This is the most common approach, but of course a variant such as
/[\D\d]/
or/[\W\w]/
could be used if you want to be unique 😆 -
Lesser Known:
/[^]/
This approach caught my eye as I was perusing MDN docs because it is so simple and yet obscure. Of course we know that normally you would prefix with a caret (
^
) and surround any characters that you dont want to match with square brackets, but by simply doing this we are saying we want any character that is not no character 😆
WARNING: This will not work in Internet Explorer (but who uses that antique?) -
Coming Soon To A Browser Near You:
/./s
This approach actually works in the latest version of Chrome. This works because of the new
dotAll
flag that is being added to JavaScript (as of the time I wrote this post).
To sum this brief post up, the most common way is also the most universal and even works for those who sadly have to support old IE users. The lesser known approach is elegant and should work in most cases since most people are not developing with IE in mind. The dotAll
approach is probably the most exciting and can be the used by any who is developing for only the newest engines that will support it, but we hope support will become more widespread shortly. If you want more information on this cool new feature, visit this 2ality blog post.
Happy coding!!! 😎