Posted By

noah on 03/30/07


Tagged

regex newline


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

shachi
dtbaker


Use a regex on a multiline string


 / Published in: JavaScript
 

URL: http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/8349ff4eef2aae8e/d3d4ba38de945951#d3d4ba38de945951

Apply string.replace() where string has line breaks, and .replace() only happens for the first line of the string! The fix is to turn newlines into unicode characters, run the regex, then turn the unicode back into newlines.

From comp.lang.javascript (thanks, Evertjan):

  1. str = "some content<script type=\"text/javascript\">var str='asdfas\ndfASDFASDF';<\/script>";
  2. str = str.replace(/\n/g,'\uffff').replace(/(<script.*?>)(.*)(?=<\/script>)/gi, function(x,y,z) {return y+z.replace(/a/gi,'Z')}).replace(/\uffff/g,'\n');

Report this snippet  

You need to login to post a comment.