Javascript search and replace


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

Why doesn't this work?


Copy this code and paste it in your HTML
  1. var str="foobar";
  2. var val="foo";
  3. var search='/'+val+'/i';
  4. alert(str.replace(search, '<strong>'+val+'</strong>'));
  5.  
  6. // it alerts "foobar" and not "<strong>foo</strong>bar"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.