title: javascript/jquery shortening a string without cutting word off
body: <p>say i had the string text = "this is a long string i cant display" i want to trim it down to 10 characters but if it doesnt end with a space finish the word i don't want the string variable to look like this "this is a long string i cant dis" i want it to finish the word until a space occurs. I'm trying this which was suggested by other people but .replace doesn't seem to be working but .length does? I read somewhere that javascript functions don't work inside jquery functions but i still don't understand why .length works</p>
  
  <pre><code>$(document).ready(function(){
      $('.article').each(function(index){
          var text = $(this).children('p').text()
          var maxLength = 6;
          var url = $(this).find('.article-link').attr('href');
  
          alert(text.replace(/^(.{1}[^\s]*).*/, "$1"));
          var trimmedString = text.substr(0, maxLength);
          var text = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")));
  
          //var text = text.substring(0, 80);
          //text = text.replace(/^(.{10}[^\s]*).*/, "$1");
  
      });
  });
  </code></pre>