Break Long, Unspaced Strings For Alert

In some browsers, such as Safari 3 and Chrome, passing a long, unspaced string into the alert function will print only 1 line of text, cutting off any part of the string exceeding the alert box width. Most of the time the alert function is used for debugging purposes, and this textual clipping can be make debugging difficult. Here is a simple RegExp that will auto-insert a newline character every 55 characters in a string, before passing the string to the alert method.

Example 1: SpacedAlert Function

spacedAlert: function(s) {
	var str =  + s;

	if (55 < str.length) {
		str = str.replace(/(\S{55})/g, $1\n);


In my personal experience, this has been most useful when checking the serialization of a form before passing it to an AJAX request. Say we have the following serialized form string:

Example 2: A Serialized Form

[email protected]

When passed to alert in Safari and Chrome, it will be truncated to:

Example 3: How Safari & Chrome Truncate

[email protected]

But the spacedAlert function will produce the following alert message:

Example 4: Spaced Alert For A Serialized Form


Now the developer can easily read the whole string.