Select first 2 words with jQuery and wrap them with <span> tag - Javascript jQuery Selector

Javascript examples for jQuery Selector:first

Description

Select first 2 words with jQuery and wrap them with <span> tag

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){/*from   w w  w . j a  va  2 s  .c  o m*/
$('p').html(function (i, html) {
    return html.replace(/(\w+\s\w+)/, '<span>$1</span><br/>')
});
    });

      </script> 
   </head> 
   <body> 
      <p>Lorem ipsum quosque tandem</p>  
   </body>
</html>

Related Tutorials