Guest User

Untitled

a guest
Feb 20th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8" />
  5.         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  6.         <style>
  7.             .nom {
  8.                 color:blue;
  9.             }
  10.         </style>
  11.     </head>
  12.     <body>
  13.         <p id="testHighlight">@ericafustero Why thank you. Your work looks awesome by the way! @treemelody</p>
  14.        
  15.         <script>
  16.             $(function() {
  17.                 jQuery.fn.highlight = function (str, className) {
  18.                     var regex = new RegExp(str, "gi");
  19.                     return this.each(function () {
  20.                         $(this).contents().filter(function() {
  21.                             return this.nodeType == 3 && regex.test(this.nodeValue);
  22.                         }).replaceWith(function() {
  23.                             return (this.nodeValue || "").replace(regex, function(match) {
  24.                                 return "<span class=\"" + className + "\">" + match + "</span>";
  25.                             });
  26.                         });
  27.                     });
  28.                 };
  29.                
  30.                 //Ici on a
  31.                 // #testHighlight : le <p></p> concerné par la fonction
  32.                 // "nom" : la classe appliquée aux mots commençant par @
  33.                 // (@[a-zA-Z0-9]{1,}) : expression régulière permettant de reconnaître les mots commençant par @
  34.                 $("#testHighlight").highlight("(@[a-zA-Z0-9]{1,})", "nom");
  35.             });
  36.            
  37.         </script>
  38.     </body>
  39. </html>
Advertisement
Add Comment
Please, Sign In to add comment