Formatter using Javascript

J

jatinder.singh

Hi All,

I am trying to Format a given TSQL code so that it is well indented and
easy to understand
It is moderately working with simple scripts, but where the script
involve some part of keywords (like endrosmentnumber has end in it)it
fails
Please guide me with the regular Expression stuff so that I can make it
work.

<textarea rows=20 cols=80 name=t1>
</textarea>
<br>
<input type=button onclick="format()" Value = "Format It">
<br>
<span id=t2 name=t2>
</span>

<script type="text/javascript">
function format()
{
var keywords = new Array("Begin Trans","Begin","case");
var keywords2 = new Array("End Trans","End");
var keywords3 = new Array("=",",");

var keywords4 = new Array("int","datetime","smalldatetime","\bfrom\b");
var keywords5 = new Array("--","set","select");
var keywords6 = new Array(",");
//var str = "Create Procedure p1 as Begin Begin Trans declare @and int
select * from p1 End Trans

End";
var str
str = t1.value
var re = new RegExp("(" + keywords.join("|") + ")", "ig");
str = str.replace(re, "<ol>$1<ol>");

var re2 = new RegExp("(" + keywords2.join("|") + ")", "ig");
str = str.replace(re2, "</ol>$1</ol>");

re2 = new RegExp("(" + keywords3.join("|") + ")", "ig");
str = str.replace(re2, "&nbsp;$1&nbsp;");

re2 = new RegExp("(" + keywords4.join("|") + ")", "ig");
str = str.replace(re2, "$1<br>");

re2 = new RegExp("(" + keywords5.join("|") + ")", "ig");
str = str.replace(re2, "<br>$1");

//document.write(str)
t2.innerHTML = str
}
</script>



With Warm regards
Jatinder Singh
 
V

VK

Hi All,

I am trying to Format a given TSQL code so that it is well indented and
easy to understand
It is moderately working with simple scripts, but where the script
involve some part of keywords (like endrosmentnumber has end in it)it
fails
Please guide me with the regular Expression stuff so that I can make it
work.

Your're asking a working solution rather than an advise. This may be
the reason you did not get an answer. How about a minimum code sample:

1) Here is my RegExp, it should find/replace such and such sequences.
2) It fails on this and this sequences.
3) How to narrow my RegExp?

And one RegExp per round would be plenty OK.
;-)
 
J

jsfromynr

Hi There,
Thanks for guidance . I should post it to some newsgroup related to
parsers can you guide me on such a parser.


With Warm regards
Jatinder Singh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top