String emphasize - Node.js HTML

Node.js examples for HTML:Element

Description

String emphasize

Demo Code


/*//from   ww w .j  a  va 2s .c  om
 * Copyright (c) 2010 Joe Pestro
 */

String.prototype.emphasize = function(q) {
  var words = q.split(" ");
  var emphasized = this;
  for (var i = 0; i < words.length; i++) {
    var exp = new RegExp(words[i], "ig");
    emphasized = emphasized.replace(exp, "<em>$&</em>");
  }
  return emphasized;
}

Related Tutorials