Your Ad Here

Posted By

Kerrick on 01/11/11


Tagged


Versions (?)

Pretty


 / Published in: JavaScript
 

  1. // Typographical fixes
  2. var replaceStart = new Date().getTime();
  3. $('body').html($('body').html()
  4. .replace(/(\d)-(\d)/ig, '$1–$2') // Proper use of en-dash for ranges
  5. .replace(/Entree/, 'Entrée') // Make Entree use the acute e
  6. .replace(/(\d)(.*?)(hours?|minutes?|seconds?$)/ig, '<span class="time">$1$2$3</span>') // Wrap <span class="time"></span> around times
  7. .replace(/(\d+)\/(\d+)/ig, '<sup>$1<\/sup>&frasl;<sub>$2<\/sub>') // Pretty fractions
  8. .replace(/(\d) <sup>/ig, '$1<sup>') // Remove the space from mixed fractions
  9. .replace(/(minutes?)/ig, '<abbr title="$1">min.</abbr>')
  10. .replace(/(hours?)/ig, '<abbr title="$1">hr.</abbr>')
  11. .replace(/(seconds?)/ig, '<abbr title="$1">sec.</abbr>')
  12. .replace(/(teaspoons?)/ig, '<abbr title="$1">t.</abbr>')
  13. .replace(/(tablespoons?)/ig, '<abbr title="$1">T.</abbr>')
  14. .replace(/(fluid ounces?)/ig, '<abbr title="$1">fl. oz.</abbr>')
  15. .replace(/(ounces(?!">))/ig, '<abbr title="ounces">oz.</abbr>') // For some reason, the s? and the (?!">) were not playing nice...
  16. .replace(/(ounce(?!s">))/ig, '<abbr title="ounces">oz.</abbr>') // ...So I had to write each line separately. Oh well.
  17. .replace(/(cups?)/ig, '<abbr title="$1">c.</abbr>')
  18. .replace(/(pints?)/ig, '<abbr title="$1">pt.</abbr>')
  19. .replace(/(quarts?)/ig, '<abbr title="$1">qt.</abbr>')
  20. .replace(/(gallons?)/ig, '<abbr title="$1">gal.</abbr>')
  21. .replace(/(pounds)/ig, '<abbr title="$1">lbs.</abbr>')
  22. .replace(/(pound(?!s">))/ig, '<abbr title="$1">lb.</abbr>') // Tricky, I had to exclude title="pounds"
  23. );
  24. var replaceEnd = new Date().getTime();
  25. var replaceTime = replaceEnd - replaceStart;
  26. console.log('It took ' + replaceTime + 'ms to format everything via regular expressions.');

Report this snippet  

You need to login to post a comment.