By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,702 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,702 IT Pros & Developers. It's quick & easy.

Formatter using Javascript

P: n/a
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

Aug 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
VK

ja************@clovertechnologies.com wrote:
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.
;-)

Aug 19 '05 #2

P: n/a
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

Aug 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.