Need to highlight the code

I tried this:

/('[^']*')|("[^"]*")/g 

But you need to find strings in quotes, but it does not look for strings in tags.

 <span class="keyword">"while"</span> 

In this example, only while should be selected.

  • 3
  • @ gx206 You would bring a piece of text in which there is what should be and what should not. And they wrote exactly what should be found. Then maybe it will be possible to make a regular schedule. But in general, since you have questions on the backlight and you do it in regular form then prepare to write a hundred regulars for different cases - Mike
  • Mike. I am looking for javascript code, php (I do the backlight of the snippet). After the operators and keywords are wrapped, tags with attributes appear in them and you don’t need to look for them. So far I have decided how you said moved the class operator to a separate regular schedule. - gx206
  • in order not to fall into such a situation, make a replacement in one pass - Grundy
  • one
    @Mike Please post your comments as an answer. - Nicolas Chabanovsky

2 answers 2

The desired regular expression looks like this :

 /(?><.+?>)*((["']).*?\2)?/g 

You unfortunately did not specify which language will work with this expression. It uses PCRE capabilities, i.e. it will work in perl and php, but JS, unfortunately, does not understand this.

In this expression:

  1. (?>) Checking the substring without capture and with the prohibition of returning back to the string. That's just how it passes the tags, not allowing the rest of the expression to capture quotes in it.
  2. (["']) Captures a double or single quote, while this is a subexpression No 2 in our regular line, so a little later in the text \2 forces us to search the string for exactly the same quote, that is, a double for a double open.

Since you said in the comments that the regular page for working with the php / js code, then it should look like this :

 /(?><.+?>)*((["'])(?:|(?:.*?)(?:[^\\]|[^\\]?(?:\\{2})+))\2)?/g 

This option normally works with shielded quotes inside strings, given the possibility of shielding the shielding character itself.

    It seems to me that regular expressions are not suitable for such tasks, it is better to use some parsers that break the text into tokens, and then you can already double the quotes, their type (single / double), etc.