A
A
Archakov Dennis2016-03-25 18:13:46
JavaScript
Archakov Dennis, 2016-03-25 18:13:46

How to implement a regular expression?

I want to make my own highlighting and everything is going fine, but these regular seasons are difficult for me :) Help me make the highlighting right.

var find = new Array(
        /(function|var|if|else|return|\&\&|\?|\:|===|==|=|\<|\>|\+\+|\-\-|\*|\%|\$|\@|\!)/g,
        /(setTimeout|appendTo)/g,
        /\/\/(.+)/g
        );

    var replacing = function(syntax,i){
    	var arr = new Array(
          "<span class=\""+ syntax +" ff\">$1</span>",
          "<span class=\""+ syntax +" fn\">$1</span>",
          "<span class=\""+ syntax +" cc\">//$1</span>"
          );
    	return arr[i];
    }

It highlights like this:
92e48ad5823449c3ab01c9f0d3ebf964.png
And it is necessary that it highlights not only, for example , $ , but the entire variable.
Who can share regular expressions, but not plugins. I've already looked and it's better to make my own than to correct someone else's code.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
khipster, 2016-03-25
@khipster

And I thought that I don’t like ready-made solutions :D

\$[a-z]+[^ ] // вместо a-z укажи все допустимые символы для переменных

S
skyfortress, 2016-03-25
@skyfortress

Try

var find = new Array(
        /(function|var|if|else|return|\&\&|\?|\:|===|==|=|\<|\>|\+\+|\-\-|\*|\%|\$\w*|\@|\!)/g,
        /(setTimeout|appendTo)/g,
        /\/\/(.+)/g
        );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question