JavaScript: truncate at period

hankknight
hankknight used Ask the Experts™
on
I use the following code to truncate text so that it is shorter than 600 characters.  However I want the truncation to happen at a period.  The result should be text that contains less than 600 characters, however the break-point should occur at the end of a sentence.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb">
<head>
<title>Hello</title>
</head>
<body>

<p id="truncateMe">As an exogenous tree grows, it creates growth rings as new wood is laid down concentrically over the old wood. In species growing in areas with seasonal climate changes, wood growth produced at different times of the year may be visible as alternating light and dark, or soft and hard, rings of wood. In temperate climates, and tropical climates with a single wet-dry season alternation, the growth rings are annual, each pair of light and dark rings being one year of growth; these are known as annual rings. In areas with two wet and dry seasons each year, there may be two pairs of light and dark rings each year; and in some (mainly semi-desert regions with irregular rainfall), there may be a new growth ring with each rainfall. In tropical rainforest regions, with constant year-round climate, growth is continuous and the growth rings are not visible nor is there a change in the wood texture. In species with annual rings, these rings can be counted to determine the age of the tree, and used to date cores or even wood taken from trees in the past, a practice is known as the science of dendrochronology. Very few tropical trees can be accurately aged in this manner. Age determination is also impossible in endogenous trees. The roots of a tree are generally embedded in earth, providing anchorage for the above-ground biomass and absorbing water and nutrients from the soil. It should be noted, however, that while ground nutrients are essential to a tree's growth the majority of its biomass comes from carbon dioxide absorbed from the atmosphere (see photosynthesis). Above ground, the trunk gives height to the leaf-bearing branches, aiding in competition with other plant species for sunlight. In many trees, the arrangement of the branches optimizes exposure of the leaves to sunlight.</p>

<script type="text/javascript">

var len = 600;
var obj = document.getElementById('truncateMe');
if (obj) {
  var trunc = obj.innerHTML;
  if (trunc.length > len) {
    /* Truncate the content of the P, then go back to the end of the
       previous word to ensure that we don't truncate in the middle of
       a word */
    trunc = trunc.substring(0, len);
    trunc = trunc.replace(/\w+$/, '');
    /* Add an ellipses to the end and make it a link that expands
       the paragraph back to its original size */
    trunc += '<a href="#" ' +
      'onclick="this.parentNode.innerHTML=' +
      'unescape(\''+escape(obj.innerHTML)+'\');return false;">' +
      '...<\/a>';
    obj.innerHTML = trunc;
  }
}

</script>

</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You should use..

trunc = trunc.replace(/[^.]+$/, '');

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial