Simple HTML Transform UBB program _ Regular expression
Last Update:2017-01-18
Source: Internet
Author: User
This function is mainly to let UBB paste the time convenient point.
1. Automatically recognize pictures, hyperlinks, font colors, and font bold, italic, underlined HTML code, and converted to UBB code.
2. Compatible with IE and Mozilla browser
<title>html Conversion Program </title>
<script language= "JavaScript1.2" >
function Htmltoubb (str) {
str = str.replace (/\r/g, "");
str = str.replace (/on (load|click|dbclick|mouseover|mousedown|mouseup) = "[^"]+ "/ig," ");
str = str.replace (/<script[^>]*?> ([\w\w]*?) <\/script>/ig, "");
str = str.replace (/<a[^>]+href= "(^"]+) "[^>]*> (. *?) <\/a>/ig, "\n[url=$1]$2[/url]\n");
str = str.replace (/<font[^>]+color= ([^ >]+) [^>]*> (. *?) <\/font>/ig, "\n[color=$1]$2[/color]\n");
str = str.replace (/]+src= "(^"]+) "[^>]*>/ig," \n[img]$1[/img]\n ");
str = str.replace (/< ([\/]?) B>/ig, "[$1b]");
str = str.replace (/< ([\/]?) Strong>/ig, "[$1b]");
str = str.replace (/< ([\/]?) U>/ig, "[$1u]");
str = str.replace (/< ([\/]?) I>/ig, "[$1i]");
str = str.replace (//g, "");
str = str.replace (/&/g, "&");
str = str.replace (/"/g," "");
str = str.replace (/</g, "<");
str = str.replace (/>/g, ">");
str = str.replace (/<br>/ig, "\ n");
str = str.replace (/<[^>]*?>/g, "");
str = str.replace (/\[url= ([^\]]+) \]\n (\[img\]\1\[\/img\)) \n\[\/url\]/g, "$");
str = str.replace (/\n+/g, "\ n");
return str;
}
function trans () {
var str = "";
str = document.getElementById (' edit '). ContentWindow.document.body.innerHTML;
if (Str.length = = 0) {
Alert ("No conversion content!") Please use CTRL + V to copy the contents to the edit box below ");
}
document.getElementById (' Recode '). Value=htmltoubb (str);
}
function Clearcode () {
document.getElementById (' Recode '). value= "";
}
function Start () {
document.getElementById (' edit '). ContentWindow.document.designMode = "on";
try {
document.getElementById (' edit '). ContentWindow.document.execCommand ("undo", False, NULL);
catch (e) {
Alert ("Your Mozilla browser does not support Online richeditor");
}
}
function Viewsource (Source) {
var Html;
if (Source) {
document.getElementById (' SCode '). Value = document.getElementById (' edit '). ContentWindow.document.body.innerHTML;
document.getElementById (' edit '). style.display= "None";
document.getElementById (' SCode '). style.display= "";
document.getElementById (' Tip ') innertext= "modify the pasted page source code >>";
document.getElementById (' Tranbutton '). disabled= "Disabled"
}
else{
document.getElementById (' edit '). ContentWindow.document.body.innerHTML = document.getElementById (' SCode '). Value;
document.getElementById (' edit '). style.display= "";
document.getElementById (' SCode '). style.display= "None";
document.getElementById (' edit '). ContentWindow.document.designMode = "on";
document.getElementById (' Tip '). innertext= "Please use CTRL + V to copy the contents here >>";
document.getElementById (' Tranbutton '). disabled= ""
}
}
</SCRIPT>
<style>
Body{background: #688ABD; margin:2px;}
Label{color: #fff;}
Th{font-size:14px;font-family:verdana, song body; Font-weight:bold;color:fc6;text-align:left}
#sCode {width:400;height:150px;border:1px solid #1F2F47; margin-top:0px!important;margin-top:-1px;margin-bottom:0 PX!important;margin-bottom:-1px;}
#ReCode {width:400;height:150px;border:1px solid #1F2F47; font-size:12px;}
#edit {width:398;height:148px;border:1px solid #1F2F47; background: #fff; font-size:12px}
. input{
border-top:1px solid #fff;
border-right:1px solid #003;
border-bottom:1px solid #003;
border-left:1px solid #fff;
Background: #F4F7FB;
}
. tip{
Color: #ffc;
Font-weight:bold;
font-size:12px;
Font-family: Song body;
}
</style>
<body onload= "Start ()" style= "" >
<table border= "0" align= "center" >
<tr>
<th style= "" >html Conversion UBB program </th>
</tr>
<tr>
<TD style= "FONT-SIZE:12PX;" >
<label for= "source" ><input type= "checkbox" id= "source" onclick= "Viewsource (this.checked)" > View source code </ Label>
<input id= "Tranbutton" type= "button" value= "Convert" onclick= "trans ()" class= "Input" >
<input type= "button" value= "clear" onclick= "Clearcode ()" class= "Input" ></td>
</tr>
<tr>
<TD class= "Tip" >
<span id= "Tip" > Please use CTRL + V to copy the content here >></span></td>
</tr>
<tr>
<td>
<textarea id= "SCode" style= "Display:none" ></textarea>
<iframe id= "edit" src= "About:blank" border= "0" frameborder= "0" ></iframe>
</td>
</tr>
<tr>
<TD class= "Tip" >
Conversion output Code >></td>
</tr>
<tr>
<td>
<textarea id= "Recode" readonly= "readonly" ></textarea>
</td>
</tr>
</table>
</body>