Writing a syntax highlighter

后端 未结 7 990
故里飘歌
故里飘歌 2020-12-23 10:28

I was hoping to write my own syntax highlighter for a summer project I am thinking of working on but I am not sure how to write my own syntax highlighter.

I know tha

7条回答
  •  不知归路
    2020-12-23 11:01

    Building a syntax highlighter is all about finding specific keywords in the code and giving them a specific style (font, font style, colour etc.). In order to achieve this, you will need to define a list of keywords specific to the programming language in which the code is written, and then parse the text (e.g. using regular expressions), find the specific tokens and replace them with properly-styled HTML tags.

    A very basic highligher written in JavaScript would look like this:

    var keywords = [ "public", "class", "private", "static", "return", "void" ];
    for (var i = 0; i < keywords.length; i++)
    {
            var regex = new RegExp("([^A-z0-9])(" + keywords[i] + ")([^A-z0-9])(?![^<]*>|[^<>]*$2$3");
    }
    

提交回复
热议问题