Coding around lack of trim() in IE’s Javascript support

IE8 doesn’t support trim() on Strings (Firefox and other browsers do). To workaround this, you can perform the same operation using some regex on a String:

example.replace(/^s+|s+$/g, '')

… this matches one or more spaces anchored at the start of the line, or one or more spaces anchored at the end of the line, replaces with ”, and then globally replaces to do both.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.