Posted By

noah on 03/30/07


regex newline

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Use a regex on a multiline string

 / Published in: JavaScript


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.