Skip to content

Instantly share code, notes, and snippets.

@kesha-antonov
Forked from kares/simpleFormat.js
Created February 25, 2016 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kesha-antonov/b1fd9c5b2a55b2a3fe43 to your computer and use it in GitHub Desktop.
Save kesha-antonov/b1fd9c5b2a55b2a3fe43 to your computer and use it in GitHub Desktop.
simple_format Ruby on Rails helper in javascript
var simpleFormatRE1 = /\r\n?/g;
var simpleFormatRE2 = /\n\n+/g;
var simpleFormatRE3 = /([^\n]\n)(?=[^\n])/g;
function simpleFormat(str) {
var fstr = str;
fstr = fstr.replace(simpleFormatRE1, "\n") // \r\n and \r -> \n
fstr = fstr.replace(simpleFormatRE2, "</p>\n\n<p>") // 2+ newline -> paragraph
fstr = fstr.replace(simpleFormatRE3, "$1<br/>") // 1 newline -> br
fstr = "<p>" + fstr + "</p>";
return fstr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment