JavaScript: Multiline Replace
Actually I just wanted to replace everything before a certain line using regular expressions. I though that was really really simple. Yes it actually is with real regular expressions as python has them or other languages. But the subset that JavaScript provides just doesn’t support the multiline modifier.
I want:
"12
34"
replace everything before “4″ with nothing, including new lines! I finally want to have:
"4"
I tried some stuff, see some playin of mine here and the final result, that does the job.
>>> "12\n34".replace(/.*3/, "")
"12
4"
>>> "12\n34".replace(/.*3/gi, "")
"12
4"
>>> "12\n34".replace(/.*3/gim, "")
"12
4"
>>> "12\n34".replace(/[\r\n]*3/gim, “”)
“124″
>>> “12\n34″.replace(/[\s\S]*3/, “”) // This works!!!
“4″
All this is copied from Firebug, using Firefox 3.0.4.
Let me explain shortly \s is for all newline and whitespace characters and \S is for all non-whitespace chars. Perfect. That works.
chris said,
May 18, 2009 at 4:34 pm
Thanks a lot, have spent almost an hour figuring this out!!!
Javascript search in replace string funkcije | .: TRSplet - internetne storitve :. said,
June 17, 2009 at 11:55 am
[...] Javascript multiline replace [...]
hamish said,
October 8, 2009 at 2:25 am
nice one! that’s really annoying. thanks for the fix
Mitchel Spradlin said,
May 3, 2011 at 5:56 pm
It’s a shame you don’t have a donate button! I’d most certainly donate to this brilliant blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will share this blog with my Facebook group. Talk soon!
Pizza Toni said,
May 3, 2011 at 6:44 pm
Hey really that what i search for. But the Download Link dont run. Wonderful Mult Line Replace.
Thanks
Kenneth Mcauly said,
May 4, 2011 at 5:53 am
I will post a website link to that web page on my blog. Thanks!
Normand Bernosky said,
May 4, 2011 at 5:54 am
Those are all great factors. Many thanks for sharing them.
Casey Duffey said,
May 4, 2011 at 6:32 am
I possess been locating a lot of diverse opinions on that and locate that submit to be one of the more enlightening on the topic. Desire we can increase the quality of responses than what I have seen as quite a few do not seem to be really applicable.
Cheap Adidas shoes said,
April 20, 2012 at 7:26 am
http://www.roseshoesoutlet2012.com/adidas-adizero-rose-10-c-2.html
Lindsey said,
April 17, 2013 at 10:47 am
I visited several sites however the audio feature for audio songs existing at this website is actually wonderful.