JavaScript Regex match all characters
JavaScript regular expressions (browser) do not have a modifier allowing the dot (.) to match all characters including the newline like the ’s’ modifier does for PERL and PCRE in PHP.
However a cool tip I learnt just recently is that using a character class and it’s negated version will allow you to match all characters. Eg: /[\s\S]/g will match all characters in a JavaScript Regular Expression.
I used this trick in the ASCII to Hex conversion JavaScript code I posted a few days ago and thought to mention it.
Joana’s drawing of Aunty T
Vara sent me a scan of Joana’s latest masterpiece :). A drawing of her Aunty T.

ASCII to Hex in JavaScript
This JavaScript will add a method to the String object, that converts the ASCII string to a hex string consisting of the equivalent character codes for each character in the String.
String.prototype.toHex = function() {
return this.replace(/[\s\S]/g, function(s) {
return parseInt(s.charCodeAt()).toString(16);
});
};
Note that hex is base 16 and ASCII is based 256 so the hex string is a longer.
I recently had to save a password in hex (Hexadecimal) instead of plain ASCII since the software that came with my Netgear USB Wireless Adapter didn’t like the ASCII version for some reason.
So for example if your password is “joe”, then to convert it to the Hex equivalent would be:
'joe'.toHex()
Oddly, my computer (Vista) took the Hex password, while the printer took the ASCII version and my friends Windows 7 took the Hex.
- cms
- drawing
- drupal
- funny
- general
- javascript
- joana
- joomla
- networking
- news
- php
- pictures
- regex
- search-engine
- security
- song
- Uncategorized
- updates
- video
- web development
- work
- xmpp
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.


