topical media & game development

talk show tell print

professional-javascript-07-TrimExample.htm / htm

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <title>Trim Example</title>
          <script type="text/javascript">
                  String.prototype.trim = function () {
                      var reExtraSpace = /^\s+(.*?)\s+/;
                      return this.replace(reExtraSpace, "$1");
                  function showText() {
                      var oInput1 = document.getElementById("txt1");
                      var oInput2 = document.getElementById("txt2");
                      oInput2.value = oInput1.value.trim();
          <p>Enter some text containing leading and trailing spaces below and click the button<br />
          <textarea rows="10" cols="50" id="txt1"></textarea><br />
          <input type="button" value="Trim" onclick="showText()" /></p>
          <p>Trimmed Text:<br />
          <textarea rows="10" cols="50" id="txt2"></textarea></p>

(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.