edlopez

Escaping special character

Apr 21st, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.05 KB | None | 0 0
  1.     <script id="allScripts">
  2.     $(document).ready(function() { // Execute when the DOM is fully loaded.
  3.         // add a shortcut method/function
  4.         (function( $ ) {
  5.             // @search: a string used to as search and replace
  6.             // @tag: tag to create wrapping element
  7.             $.fn.wrapHanzi = function(search, tag, replace) {
  8.                 return this.each(function() {
  9.                     regex = new RegExp(search, 'g'); // create a regex on the fly, See: http://is.gd/BfR9gR
  10.                     if (!replace) { replace = search; }
  11.                     wrap = '<'+tag+'>'+replace+'</'+tag+'>';
  12.  
  13.                     $(this).replaceText( regex, wrap);
  14.                 });
  15.             };
  16.         })( jQuery );
  17.  
  18.         $("#text").wrapHanzi('水', 'easy');
  19.         $("span").wrapHanzi('韓', 'unknow');
  20.         $("span").wrapHanzi('國', 'partial');
  21.         $("span").wrapHanzi('有', 'easy');
  22.         $("span").wrapHanzi('\\[', 'easy', '['); # 3 parameters
  23.         $("span").wrapHanzi('\\]', 'easy', ']');
  24.     });
  25.     </script>
Advertisement
Add Comment
Please, Sign In to add comment