Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How to use 'not in' with RegExp

    hi amphiluke (thanks for your guide) and All the masters...

    I have problem from the code (it post in this thread with some of little modification):
    Code:
    <!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:
    Code:
    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:
    Code:
    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:
    Code:
    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

  • #2
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up

    Sorry I'm newbie here can you explain it with code... sorry... Oh thanks for your reply any way

  • #4
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    Hi there - just swap these around:


    Code:
    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

  • #5
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have tried that and the result id like this:
    Code:
    If iVar = 1 Then
      Text1.Text = "Satu" 'This is comment
    ElseIf iVar = 2 Then
      Text1.Text = "Dua"
    End If

  • #6
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Hi, orgbiz.
    Here is an improved example.
    Code:
    <!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>
    I am still learning English

  • Users who have thanked Amphiluke for this post:

    orgbiz (08-21-2009)

  • #7
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by orgbiz View Post
    I have tried that and the result id like this:
    Code:
    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

  • #8
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry for my late response Amphiluke, Thanks again this working...
    You're the best men
    Say THANKS to others who posted useful post for you.
    Edit your post title with Resolved prefix if your problem have been resolved, so you will also help others with that.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •