Search code examples
phpjavascriptjqueryregexjeditable

Jeditable doubling up on linebreaks every edit


I'm using

var retval = value.replace(/<br[\s\/]?>/gi, '\n');

To strip the <br> tags from the textarea and nl2br('$_POST('newValueHere')') to insert into my database and to return back to jeditable to display the edits. The only problem I'm having is that each click on the editable field seems to make all the <br> tags be written twice? This makes no sense, does anyone have any ideas what could be going on?


Solution

  • nl2br doesn't replace linefeeds, it inserts <br> tags before them. Your regex turns <br>\n to \n\n, then nl2br turns that into <br>\n<br>\n. You need to remove the linefeed following the tag if there is one:

    var retval = value.replace(/<br\s*\/?>\n?/gi, '\n');