Testing with multiple regular expressions at the same time (for use in parsing)

I am writing a simple syntax shortcut in JavaScript, and I need to find a way to test with multiple regular expressions at the same time.

The idea is to figure out what comes first, so I can define a new set of expressions to search for.

Expressions can be something like this:

/<%@/, /<%--/, /<!--/And/<[a-z:-]/

First I tried a strategy in which I combined expressions in groups such as:

/(<%@)|(<%--)|(<!--)|(<[a-z:-])/

Thus, I was able to find out which group was not undefined. But the problem is that some subexpressions contain groups or backrefferences.

So my question is this:

Does anyone know a good and reasonable way of finding matches with multiple regular expressions in a string?

+3
2

- , , , , ? , . if-elseif :

if (token.startsWith("<%@")) {
  // paint it red
}
else if (token.startsWith("<%--")) {
  // paint it green
}
else if (token.startsWith("<!--")) {
  // paint it blue
}
else if (token.matches("^<[a-z:-]")) {
  // paint it black
}

JavaScript. .

+5

ANTLR - . JavaScript http://code.google.com/p/antlr-javascript/

Welbog , , , JavaScript, ANTLR.

+2

Source: https://habr.com/ru/post/1726792/


All Articles