Javascript RegEx problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr.Clean

    Javascript RegEx problem

    I am working on modifying a syntax highlighter written in
    javascript and it uses several regexes. I need to add a
    language to the avail highlighters and need the following regexes
    modified to parse the new language, Delphi/Pascal. Source to the
    highlighter is avail here:



    *************** *************** *************** *
    COMMENTS
    *************** *************** *************** *
    regex = new RegExp('//.*$|/\\*[^\\*]*(.)*?\\*/', 'gm');
    Matches- single line comment: '// ' until end of line
    multi line comment '{' until closing '}'
    Example:

    // This is a Comment

    {
    This is
    a multiline comment
    }

    *************** *************** *************** *
    STRINGS
    *************** *************** *************** *
    regex = new RegExp('"(?:\\. |[^\\""])*"', 'g');
    Matches - any text enclosed in single quotes
    if the string contains single quotes it will be
    escaped like this:
    'isn''t that grand'
    Example:

    'this is a string'

    *************** *************** *************** *
    DIRECTIVES/REGIONS
    *************** *************** *************** *
    regex = new RegExp('^\\s*{/$.*', 'gm');
    Matches - '{' followed by '$' followed by 1+ Alpha chars
    closed with '}'
    Example:

    {$IFDEF YAHOO} // this is a directive
    {$ELSE YAHOO} // this is a directive
    {$ENDDEF YAHOO} // this is a directive

    {$R *.res} // this is a directive

    *************** *************** *************** *
    NUMBERS
    *************** *************** *************** *
    regex = new RegExp('/[^\d]/gi', 'g');
    Matches - integer = numeric chars only
    float = numerics and decimal only, no commas
    hex = '$' foloewd by any combination of 'A'-'F'
    and/or 0-9
    Example:

    x := x + 1234; // example of a number
    y := y + 1.0; // example of a float number
    z := $FFAA; // example of a hex number


    for instance, in the first example:
    regex = new RegExp('//.*$|/\\*[^\\*]*(.)*?\\*/', 'gm');
    Matches - single line comment: '// ' until end of line
    multi line comment '{' until closing '}'
    Example:

    // This is a Comment

    {
    This is
    a multiline comment
    }


    the line:
    regex = new RegExp('//.*$|/\\*[^\\*]*(.)*?\\*/', 'gm');

    is the current regex.

    the line(s):
    - single line comment: '// ' until end of line
    multi line comment '{' until closing '}'

    spell out what I want the regex to match

    and this:
    Example:

    // This is a Comment

    {
    This is
    a multiline comment
    }


    Here is another one that I can't figure out.

    Source:
    asm
    MOV AX,1234H
    MOV Number,AX
    end;

    Need to match everything between the 'asm' and 'end;'
  • Dr John Stockton

    #2
    Re: Javascript RegEx problem

    JRS: In article <1107269716.711 be950c458361567 b8868ee0225138@ teranews>,
    dated Tue, 1 Feb 2005 08:55:14, seen in news:comp.lang. javascript, Mr.
    Clean <mrclean@p&g.co m> posted :[color=blue]
    >I am working on modifying a syntax highlighter written in
    >javascript and it uses several regexes. I need to add a
    >language to the avail highlighters and need the following regexes
    >modified to parse the new language, Delphi/Pascal. Source to the
    >highlighter is avail here:
    >http://www.dreamprojections.com/Synt...r/Default.aspx
    >
    >
    >************** *************** *************** **
    >COMMENTS
    >************** *************** *************** **
    >regex = new RegExp('//.*$|/\\*[^\\*]*(.)*?\\*/', 'gm');
    > Matches- single line comment: '// ' until end of line
    > multi line comment '{' until closing '}'[/color]

    Needs also to support comment (* ... *).
    One type of comment can contain the opening symbol for another.

    [color=blue]
    >************** *************** *************** **
    >DIRECTIVES/REGIONS
    >************** *************** *************** **
    >regex = new RegExp('^\\s*{/$.*', 'gm');
    > Matches - '{' followed by '$' followed by 1+ Alpha chars
    > closed with '}'[/color]

    Also (*$ ... *)

    [color=blue]
    >************** *************** *************** **
    >NUMBERS
    >************** *************** *************** **
    >regex = new RegExp('/[^\d]/gi', 'g');
    > Matches - integer = numeric chars only
    > float = numerics and decimal only, no commas
    > hex = '$' foloewd by any combination of 'A'-'F'
    > and/or 0-9[/color]

    Also needs to handle such as 3.5e4 6.99E-07 .

    Note that, to aid Continentals, (. and .) can be used for [ and ] -
    there may be others similar that I have forgotten.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
    <URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
    <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

    Comment

    • Mr.Clean

      #3
      Re: Javascript RegEx problem

      <SNIP>
      [color=blue]
      > Also needs to handle such as 3.5e4 6.99E-07 .
      >
      > Note that, to aid Continentals, (. and .) can be used for [ and ] -
      > there may be others similar that I have forgotten.[/color]

      You know John, I forgot that you hangout here, too.
      I am not even familiar with regex expressions. This is
      a mod to allow a highlighter, noted below, to highlight Delphi
      code.

      The author was familiar with Delphi and got one working but
      a few things were left out. The things you noted above and
      asm blocks. I know that a parser will probably need to be
      written to do the asm block kind of like how he did XML.

      I know you know Delphi so would you be kind enough to help out?


      Comment

      Working...