Skip to content

Greedy and lazy quantifiers #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

The result is: `match:123 4`.
El resultado es: `match:123 4`.

First the lazy `pattern:\d+?` tries to take as little digits as it can, but it has to reach the space, so it takes `match:123`.
Primero el perezoso `pattern:\d+?` trata de tomar la menor cantidad de dígitos posible, pero tiene que llegar al espacio, por lo que toma `match:123`.

Then the second `\d+?` takes only one digit, because that's enough.
Después el segundo `\d+?` toma solo un dígito, porque es sufuciente.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A match for /d+? d+?/
# Una coincidencia para /d+? d+?/

What's the match here?
¿Cual es la coincidencia aquí?

```js
"123 456".match(/\d+? \d+?/g) ); // ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
We need to find the beginning of the comment `match:<!--`, then everything till the end of `match:-->`.
Necesitamos encontrar el inicio del comentario `match:<!--`, después todo hasta el fin de `match:-->`.

An acceptable variant is `pattern:<!--.*?-->` -- the lazy quantifier makes the dot stop right before `match:-->`. We also need to add flag `pattern:s` for the dot to include newlines.
Una variante aceptable es `pattern:<!--.*?-->` -- el cuantificador perezoso detiene el punto justo antes de `match:-->`. También necesitamos agregar la bandera `pattern:s` al punto para incluir líneas nuevas.

Otherwise multiline comments won't be found:
De lo contrario, no se encontrarán comentarios multilínea:

```js run
let regexp = /<!--.*?-->/gs;

let str = `... <!-- My -- comment
test --> .. <!----> ..
let str = `... <!-- Mi -- comentario
prueba --> .. <!----> ..
`;

alert( str.match(regexp) ); // '<!-- My -- comment \n test -->', '<!---->'
alert( str.match(regexp) ); // '<!-- Mi -- comentario \n prueba -->', '<!---->'
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Find HTML comments
# Encuentra el comentario HTML

Find all HTML comments in the text:
Encuentra todos los comentarios HTML en el texto:

```js
let regexp = /your regexp/g;

let str = `... <!-- My -- comment
test --> .. <!----> ..
let str = `... <!-- Mi -- comentario
prueba --> .. <!----> ..
`;

alert( str.match(regexp) ); // '<!-- My -- comment \n test -->', '<!---->'
alert( str.match(regexp) ); // '<!-- Mi -- comentario \n prueba -->', '<!---->'
```
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

The solution is `pattern:<[^<>]+>`.
La solución es `pattern:<[^<>]+>`.

```js run
let regexp = /<[^<>]+>/g;

let str = '<> <a href="/"> <input type="radio" checked> <b>';
let str = '<> <a href="/"> <input type="radio" comprobado> <b>';

alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" comprobado>', '<b>'
```
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Find HTML tags
# Encontrar las etiquetas HTML

Create a regular expression to find all (opening and closing) HTML tags with their attributes.
Crear una expresión regular para encontrar todas las etiquetas HTML (de apertura y cierre) con sus atributos.

An example of use:
Un ejemplo de uso:

```js run
let regexp = /your regexp/g;
let regexp = /tu regexp/g;

let str = '<> <a href="/"> <input type="radio" checked> <b>';
let str = '<> <a href="/"> <input type="radio" comprobado> <b>';

alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" comprobado>', '<b>'
```

Here we assume that tag attributes may not contain `<` and `>` (inside squotes too), that simplifies things a bit.
Asumimos que los atributos de etiqueta no deben contener `<` ni `>` (dentro de comillas dobles también), esto simplifica un poco las cosas.
Loading