Regular Expression Wrap


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var term = 'Valves';
  2. var value = 'Foo Foo Valves Blah Yo Yo';
  3.  
  4. var regex = new RegExp(term,'ig');
  5.  
  6. var replaced = value.replace(regex,'<span>$&</span>');
  7.  
  8. alert(replaced); //Foo Foo <span>Valves</span> Blah Yo Yo

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.