PDA

View Full Version : How to use 'not in' with RegExp


orgbiz
08-19-2009, 08:43 AM
hi amphiluke (http://www.codingforums.com/member.php?u=80538) (thanks for your guide) and All the masters...

I have problem from the code (it post in this thread (http://www.codingforums.com/showthread.php?p=855171) with some of little modification):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>awSimpleCodeHighlight</title>
<script type="text/javascript">
var vbKeywords = new Array("If","Then","Else","ElseIf","End");
function HiLightCode(codeBlock) {
var StrCode = codeBlock.innerHTML;
var re = / /g;
StrCode = StrCode.replace(re, "&nbsp;");
for (var i = 0; i < vbKeywords.length; i++) {
var re = new RegExp("\\b" + vbKeywords[i] + "\\b", "gi");
StrCode = StrCode.replace(re, "<font color=\"#0000FF\">" + vbKeywords[i] + "</font>");
delete re;
}

var lines = StrCode.split("\n");
var outputCode = "";
for (var i = 0; i < lines.length; i++) {
var re = new RegExp("('.+)");
outputCode += lines[i].replace(re, "<font color='#008000'>$1</font>") + "<br />";
delete re;
}
codeBlock.innerHTML = outputCode +"<table cellspacing='0'><tr><td><font size='1%'><a href='http://www.vb6-id.co.cc/'>awSimpleCodeHighLight &copy; 2009 vb6-id.co.cc</a></font></td></tr></table><br />";
}
function HiLightAll() {
var StrCodes = document.getElementsByTagName("code");
for (var i = 0; i < StrCodes.length; i++) {
if (StrCodes[i].className == "vb") HiLightCode(StrCodes[i]);
}
}
window.onload=HiLightAll;
</script>

</head>

<body>
<div>
<code class="vb">
If iVar = 1 Then
Text1.Text = "Satu" 'This is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If
</code>
<code class="vb">
If iVar = 1 Then
Text1.Text = "Satu" 'This is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If
</code>
</div>
</body>
</html>


The problem is if I change the keywords array become like this one:

var vbKeywords = new Array("Abs", "Access", "AddItem", "AddNew", "Alias", "And", "Any", "App", "AppActivate", "Append", "AppendChunk", "Arrange", "As", "Asc", "Atn", "Base", "Beep", "BeginTrans", "Binary", "ByVal", "Call", "Case", "CBool", "CCur", "CDbl", "ChDir", "ChDrive", "Chr", "Chr$", "CInt", "Circle", "Clear", "Clipboard", "CLng", "Close", "Cls", "Command", "Command$", "CommitTrans", "Compare", "Const", "Control", "Controls", "Cos", "CreateDynaset", "CSng", "CStr", "CurDir$", "Currency", "CVar", "CVDate", "Data", "Date", "Date$", "DateSerial", "DateValue", "Day", "Debug", "Declare", "DefCur", "CefDbl", "DefInt", "DefLng", "DefSng", "DefStr", "DefVar", "Delete", "Dim", "Dir", "Dir$", "Do", "DoEvents", "Double", "Drag", "Dynaset", "Each", "Edit", "Else", "ElseIf", "End", "EndDoc", "Environ$", "EOF", "Eqv", "Erase", "Erl", "Err", "Error", "Error$", "ExecuteSQL", "Exit", "Exp", "Explicit", "False", "FieldSize", "FileAttr", "FileCopy", "FileDateTime", "FileLen", "Fix", "For", "Form", "Format", "Format$", "Forms", "FreeFile", "Function", "Get", "GetAttr", "GetChunk", "GetData", "DetFormat", "GetText", "Global", "GoSub", "GoTo", "Hex", "Hex$", "Hide", "Hour", "If", "Imp", "In", "Input", "Input$", "InputBox", "InputBox$", "InStr", "Int", "Integer", "Is", "IsDate", "IsEmpty", "IsNull", "IsNumeric", "Kill", "LBound", "LCase", "LCase$", "Left", "Left$", "Len", "Let", "Lib", "Like", "Line", "LinkExecute", "LinkPoke", "LinkRequest", "LinkSend", "Load", "LoadPicture", "Loc", "Local", "Lock", "LOF", "Log", "Long", "Loop", "LSet", "LTrim", "LTrim$", "Me", "Mid", "Mid$", "Minute", "MkDir", "Mod", "Month", "Move", "MoveFirst", "MoveLast", "MoveNext", "MovePrevious", "MoveRelative", "MsgBox", "Name", "New", "NewPage", "Next", "NextBlock", "Not", "Nothing", "Now", "Null", "Oct", "Oct$", "On", "Open", "OpenDataBase", "Option", "Or", "Output", "Point", "Preserve", "Print", "Printer", "PrintForm", "Private", "PSet", "Put", "QBColor", "Random", "Randomize", "Read", "ReDim", "Refresh", "RegisterDataBase", "Rem", "RemoveItem", "Reset", "Restore", "Resume", "Return", "RGB", "Right", "Right$", "RmDir", "Rnd", "Rollback", "RSet", "RTrim", "RTrim$", "SavePicture", "Scale", "Second", "Seek", "Select", "SendKeys", "Set", "SetAttr", "SetData", "SetFocus", "SetText", "Sgn", "Shared", "Shell", "Show", "Sin", "Single", "Space", "Space$", "Spc", "Sqr", "Static", "Step", "Stop", "Str", "Str$", "StrComp", "String", "String$", "Sub", "System", "Tab", "Tan", "TextHeight", "TextWidth", "Then", "Time", "Time$", "Timer", "TimeSerial", "TimeValue", "To", "Trim", "Trim$", "True", "Type", "TypeOf", "UBound", "UCase", "UCase$", "Unload", "Unlock", "Until", "Update", "Using", "Val", "Variant", "VarType", "Weekday", "Wend", "With", "While", "Width", "Write", "Xor", "Year", "ZOrder");


the result become like this:

If iVar = 1 Then
Text1.Text = "Satu" 'This Is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If


And the result that I want is like this:

If iVar = 1 Then
Text1.Text = "Satu" 'This Is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If


do you (or anyone) have a solution for my problem?

THXs for any reply

eddjc
08-19-2009, 08:56 AM
you could just change the order in which you do the changes - i.e. first change the colour for the keyword, then change the colour for the comment?

Another way you could do it is define ranges i.e if a comment starts at 45 and is 30 long, then when you go to highlight a word, you first check it against array of comment ranges...

I think the first method is less wasteful, but also less flexible...
Cheers
Edd

orgbiz
08-19-2009, 09:03 AM
Sorry I'm newbie here can you explain it with code...:confused: sorry...;) Oh thanks for your reply any way

eddjc
08-19-2009, 09:17 AM
Hi there - just swap these around:


StrCode = StrCode.replace(re, "&nbsp;");

var lines = StrCode.split("\n");


var outputCode = "";

for (var i = 0; i < lines.length; i++) {
var re = new RegExp("('.+)");

outputCode += lines[i].replace(re, "<font color='#008000'>$1</font>") + "<br />";
delete re;
}

for (var i = 0; i < vbKeywords.length; i++) {

var re = new RegExp("\\b" + vbKeywords[i] + "\\b", "gi");

StrCode = StrCode.replace(re, "<font color=\"#0000FF\">" + vbKeywords[i] + "</font>");

delete re;
}



Cheers
Edd

orgbiz
08-19-2009, 09:38 AM
I have tried that and the result id like this:

If iVar = 1 Then
Text1.Text = "Satu" 'This is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If

Amphiluke
08-19-2009, 12:37 PM
Hi, orgbiz.
Here is an improved example.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Template</title>

<style type="text/css">
.hilightkw {
color: #0000FF;
}
.hilightcomm {
color: #008000;
}
</style>

<script type="text/javascript">
var vbKeywords = new Array("Abs", "Access", "AddItem", "AddNew", "Alias", "And", "Any", "App", "AppActivate", "Append", "AppendChunk", "Arrange", "As", "Asc", "Atn", "Base", "Beep", "BeginTrans", "Binary", "ByVal", "Call", "Case", "CBool", "CCur", "CDbl", "ChDir", "ChDrive", "Chr", "Chr$", "CInt", "Circle", "Clear", "Clipboard", "CLng", "Close", "Cls", "Command", "Command$", "CommitTrans", "Compare", "Const", "Control", "Controls", "Cos", "CreateDynaset", "CSng", "CStr", "CurDir$", "Currency", "CVar", "CVDate", "Data", "Date", "Date$", "DateSerial", "DateValue", "Day", "Debug", "Declare", "DefCur", "CefDbl", "DefInt", "DefLng", "DefSng", "DefStr", "DefVar", "Delete", "Dim", "Dir", "Dir$", "Do", "DoEvents", "Double", "Drag", "Dynaset", "Each", "Edit", "Else", "ElseIf", "End", "EndDoc", "Environ$", "EOF", "Eqv", "Erase", "Erl", "Err", "Error", "Error$", "ExecuteSQL", "Exit", "Exp", "Explicit", "False", "FieldSize", "FileAttr", "FileCopy", "FileDateTime", "FileLen", "Fix", "For", "Form", "Format", "Format$", "Forms", "FreeFile", "Function", "Get", "GetAttr", "GetChunk", "GetData", "DetFormat", "GetText", "Global", "GoSub", "GoTo", "Hex", "Hex$", "Hide", "Hour", "If", "Imp", "In", "Input", "Input$", "InputBox", "InputBox$", "InStr", "Int", "Integer", "Is", "IsDate", "IsEmpty", "IsNull", "IsNumeric", "Kill", "LBound", "LCase", "LCase$", "Left", "Left$", "Len", "Let", "Lib", "Like", "Line", "LinkExecute", "LinkPoke", "LinkRequest", "LinkSend", "Load", "LoadPicture", "Loc", "Local", "Lock", "LOF", "Log", "Long", "Loop", "LSet", "LTrim", "LTrim$", "Me", "Mid", "Mid$", "Minute", "MkDir", "Mod", "Month", "Move", "MoveFirst", "MoveLast", "MoveNext", "MovePrevious", "MoveRelative", "MsgBox", "Name", "New", "NewPage", "Next", "NextBlock", "Not", "Nothing", "Now", "Null", "Oct", "Oct$", "On", "Open", "OpenDataBase", "Option", "Or", "Output", "Point", "Preserve", "Print", "Printer", "PrintForm", "Private", "PSet", "Put", "QBColor", "Random", "Randomize", "Read", "ReDim", "Refresh", "RegisterDataBase", "Rem", "RemoveItem", "Reset", "Restore", "Resume", "Return", "RGB", "Right", "Right$", "RmDir", "Rnd", "Rollback", "RSet", "RTrim", "RTrim$", "SavePicture", "Scale", "Second", "Seek", "Select", "SendKeys", "Set", "SetAttr", "SetData", "SetFocus", "SetText", "Sgn", "Shared", "Shell", "Show", "Sin", "Single", "Space", "Space$", "Spc", "Sqr", "Static", "Step", "Stop", "Str", "Str$", "StrComp", "String", "String$", "Sub", "System", "Tab", "Tan", "TextHeight", "TextWidth", "Then", "Time", "Time$", "Timer", "TimeSerial", "TimeValue", "To", "Trim", "Trim$", "True", "Type", "TypeOf", "UBound", "UCase", "UCase$", "Unload", "Unlock", "Until", "Update", "Using", "Val", "Variant", "VarType", "Weekday", "Wend", "With", "While", "Width", "Write", "Xor", "Year", "ZOrder");
function HiLightCode(codeBlock) {
var _code = codeBlock.innerHTML;
var re = / /g;
_code = _code.replace(re, "&nbsp;");
for (var i = 0; i < vbKeywords.length; i++) {
var re = new RegExp("(\\b)" + vbKeywords[i] + "(\\b)", "gi");
_code = _code.replace(re, "$1<span class=\"hilightkw\">" + vbKeywords[i] + "</span>$2");
delete re;
}
var lines = _code.split("\n");
var outputCode = "";
var auxStr = "";
var re2 = new RegExp("<span class=\"hilightkw\">([^<]*)</span>", "gi");
for (var i = 0; i < lines.length; i++) {
var re = new RegExp("('.+)");
if (re.exec(lines[i]) != null) {
auxStr = RegExp.$1;
auxStr = auxStr.replace(re2, "$1");
outputCode += lines[i].slice(0, lines[i].indexOf("'")) + "<span class=\"hilightcomm\">" + auxStr + "</span><br />";
} else {
outputCode += lines[i] + "<br />";
}
delete re;
}
codeBlock.innerHTML = outputCode;
}
function HiLightAll() {
var _codes = document.getElementsByTagName("pre");
for (var i = 0; i < _codes.length; i++) {
if (_codes[i].className == "vb") HiLightCode(_codes[i]);
}
}
window.onload=HiLightAll;
</script>

</head>

<body>
<div>
<pre class="vb">
If iVar = 1 And iVar = 1 Then
Text1.Text = "Satu" 'This is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If
</pre>
<pre class="vb">
If iVar = 1 Then
Text1.Text = "Satu" 'This is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If
</pre>
</div>

</body>

</html>

eddjc
08-19-2009, 12:41 PM
I have tried that and the result id like this:

If iVar = 1 Then
Text1.Text = "Satu" 'This is comment
ElseIf iVar = 2 Then
Text1.Text = "Dua"
End If


oh! Well it worked for me...

Edd

orgbiz
08-21-2009, 10:43 AM
Sorry for my late response Amphiluke, Thanks again this working...
You're the best men ;)