Created
August 8, 2013 11:50
-
-
Save soyuka/6183947 to your computer and use it in GitHub Desktop.
Html To BBCode javascript function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Adapted from http://skeena.net/htmltobb/ | |
var htmlToBBCode = function(html) { | |
html = html.replace(/<pre(.*?)>(.*?)<\/pre>/gmi, "[code]$2[/code]"); | |
html = html.replace(/<h[1-7](.*?)>(.*?)<\/h[1-7]>/, "\n[h]$2[/h]\n"); | |
//paragraph handling: | |
//- if a paragraph opens on the same line as another one closes, insert an extra blank line | |
//- opening tag becomes two line breaks | |
//- closing tags are just removed | |
// html += html.replace(/<\/p><p/<\/p>\n<p/gi; | |
// html += html.replace(/<p[^>]*>/\n\n/gi; | |
// html += html.replace(/<\/p>//gi; | |
html = html.replace(/<br(.*?)>/gi, "\n"); | |
html = html.replace(/<textarea(.*?)>(.*?)<\/textarea>/gmi, "\[code]$2\[\/code]"); | |
html = html.replace(/<b>/gi, "[b]"); | |
html = html.replace(/<i>/gi, "[i]"); | |
html = html.replace(/<u>/gi, "[u]"); | |
html = html.replace(/<\/b>/gi, "[/b]"); | |
html = html.replace(/<\/i>/gi, "[/i]"); | |
html = html.replace(/<\/u>/gi, "[/u]"); | |
html = html.replace(/<em>/gi, "[b]"); | |
html = html.replace(/<\/em>/gi, "[/b]"); | |
html = html.replace(/<strong>/gi, "[b]"); | |
html = html.replace(/<\/strong>/gi, "[/b]"); | |
html = html.replace(/<cite>/gi, "[i]"); | |
html = html.replace(/<\/cite>/gi, "[/i]"); | |
html = html.replace(/<font color="(.*?)">(.*?)<\/font>/gmi, "[color=$1]$2[/color]"); | |
html = html.replace(/<font color=(.*?)>(.*?)<\/font>/gmi, "[color=$1]$2[/color]"); | |
html = html.replace(/<link(.*?)>/gi, ""); | |
html = html.replace(/<li(.*?)>(.*?)<\/li>/gi, "[*]$2"); | |
html = html.replace(/<ul(.*?)>/gi, "[list]"); | |
html = html.replace(/<\/ul>/gi, "[/list]"); | |
html = html.replace(/<div>/gi, "\n"); | |
html = html.replace(/<\/div>/gi, "\n"); | |
html = html.replace(/<td(.*?)>/gi, " "); | |
html = html.replace(/<tr(.*?)>/gi, "\n"); | |
html = html.replace(/<img(.*?)src="(.*?)"(.*?)>/gi, "[img]$2[/img]"); | |
html = html.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, "[url=$2]$4[/url]"); | |
html = html.replace(/<head>(.*?)<\/head>/gmi, ""); | |
html = html.replace(/<object>(.*?)<\/object>/gmi, ""); | |
html = html.replace(/<script(.*?)>(.*?)<\/script>/gmi, ""); | |
html = html.replace(/<style(.*?)>(.*?)<\/style>/gmi, ""); | |
html = html.replace(/<title>(.*?)<\/title>/gmi, ""); | |
html = html.replace(/<!--(.*?)-->/gmi, "\n"); | |
html = html.replace(/\/\//gi, "/"); | |
html = html.replace(/http:\//gi, "http://"); | |
html = html.replace(/<(?:[^>'"]*|(['"]).*?\1)*>/gmi, ""); | |
html = html.replace(/\r\r/gi, ""); | |
html = html.replace(/\[img]\//gi, "[img]"); | |
html = html.replace(/\[url=\//gi, "[url="); | |
html = html.replace(/(\S)\n/gi, "$1 "); | |
return html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
354reerweswdsaf