it's all about Regular expressions; Regular expressions are a very powerful tool for performing pattern matches in strings. You can perform complex tasks that once required lengthy procedures with just a few lines of code using regular expressions...



in this example,i made a simple Code Beautifier and formatter for VB/VBScript/VB.net using JavaScript and CSS.



in this syntax highlighter There are patterns for matching Comments lines,Quoted strings,Escape HTML tags,and Language Keywords..



To extend this to format C# for example; all you need to change is the keywords of that programming language at the line

s = keywords_Beautifier("New|Class|..",s);

where you should place language keywords as one string separated by | as the first parameter



Go ahead try it your self









Output will look like this



include this css

<style>

b.KW {

color:blue;

}

b.Cm , b.Cm b.KW , b.Cm b.QS {

color:green;

font-weight:lighter;

}

b.QS, b.QS b.KW {

color:maroon;

font-weight:lighter;

}

</style>



Code Beautifier And Formatter Source Code

<SCRIPT LANGUAGE=javascript>

function Code_Beautifier(){

var a = document.getElementById("codearea");

if (!a) return;

var s = a.value;

//escape HTML Tags <>

s= s.replace(/</g ,"<");

s= s.replace(/>/g ,">");

//next will use regular expressions patterns, and surround matches with <b ClssS="[Css class]"></b>

// but note that 'ClssS' is misspelled so it don't get changed by keywords pattern

//Beautify a quoted string

s= s.replace(/(\"[^\"]*\")/gi ,'<b ClssS="qs">$1</b>');

//Beautify Comments

s = Comments_Beautifier(s);

//escape lines

s= s.replace(/

/g ,"<br />");

//escape spaces

s= s.replace(/\s\s/gi ," ");

//Beautify keywords

s = keywords_Beautifier("New|Class|Shared|Protected|Friend|byval|byREF|Optional|RETURN|GET|Property|Erase|LBound|UBound|Let|Set|Rem|Const|Empty|Nothing|Null|True|False|Control|Do|Loop|For|Next|For|If|Then|Else|select|While|Wend|end|Abs|Asc|AscB|AscW|Chr|ChrB|ChrW|CBool|CByte|CDate|CDbl|Cint|CLng|CSng|CStr|DateSerial|DateValue|Hex|Oct|Fix|Int|Sgn|TimeSerial|TimeValue|date|Time|DateSerial|DateValue|Day|Month|Weekday|Year|Hour|Minute|Second|Now|TimeSerial|TimeValue|Dim|Private|Public|ReDim|Sub|On|Err|InputBox|MsgBox|Atn|Cos|Sin|Tan|Exp|Log|Sqr|Randomize|Rnd|Mod|Is|And|Or|Xor|Eqv|Imp|CreateObject|IsObject|option|Call|FUNCTION|Sub|Instr|InStrB|Len|LenB|Lcase|Ucase|Left|LeftB|Mid|MidB|Right|RightB|Space|StrComp|string|Ltrim|Rtrim|Trim|IsArray|IsDate|IsEmpty|IsNull|IsNumeric|IsObject|VarType|ERROR",s);

s = CorrectCssClass(s);

var o = document.getElementById("output") ;

if (o) o.innerHTML = s;

var oa = document.getElementById("outarea");

if (oa) oa.value=s;

}

function keywords_Beautifier(keys,s) {

var rx = new RegExp("(\\b)("+ keys +")(\\b)", "gi")

return s.replace(rx ,'$1<b ClssS="Kw">$2</b>$3');

}

function Comments_Beautifier(s) {

return s.replace(/\'([^

]+)

/gi ,'<b ClssS="Cm">\'$1</b>

');

}

function CorrectCssClass(s) {

return s.replace(/ClssS/g ,'class');

}

</SCRIPT>

Some fine JavaScript Regular Expressions References

Copy & paste thisYou will need to