Discuss Scratch

djdolphin
Scratcher
1000+ posts

Is there an alternative to regexp lookbehind in javascript?

I wrote this javascript code:
data = data.replace(/(?<!>)\n/g, "<br>");
It's supposed to replace all newlines not preceded by > with <br>, but it looks like javascript's regexp engine doesn't support lookbehinds. Does anyone know an alternative I could use?

Last edited by djdolphin (April 1, 2014 01:06:29)


!
Hardmath123
Scratcher
1000+ posts

Is there an alternative to regexp lookbehind in javascript?

 /[^>]\n/g 
scimonster
Scratcher
1000+ posts

Is there an alternative to regexp lookbehind in javascript?

Maybe you should ask this on Stack Overflow?

Retired Community Moderator
BTW, i run Google Chrome 41.0.2272.101 on a Linux system - Ubuntu 14.04. NEW: iPad 4th gen. w/retina.

418 I'm a teapot (original - to be read by bored computer geeks)
THE GAME (you just lost)
; THE SEMICOLON LIVES ON IN OUR SIGS
nXIII
Scratcher
1000+ posts

Is there an alternative to regexp lookbehind in javascript?

Hardmath123 wrote:

 /[^>]\n/g 
That doesn't work for initial newlines and doesn't keep the preceding character. You can do something like
replace(/([^>]|^)\n/g, '$1<br>')

nXIII · GitHub
djdolphin
Scratcher
1000+ posts

Is there an alternative to regexp lookbehind in javascript?

nXIII wrote:

Hardmath123 wrote:

 /[^>]\n/g 
That doesn't work for initial newlines and doesn't keep the preceding character. You can do something like
replace(/([^>]|^)\n/g, '$1<br>')
Thanks, it works great!

!
Harakou
Scratcher
1000+ posts

Is there an alternative to regexp lookbehind in javascript?

Closed by request of owner.

Powered by DjangoBB