diff --git a/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md b/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md index 04cca7c86..12d5376da 100644 --- a/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md +++ b/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md @@ -1,130 +1,130 @@ -# Lookahead and lookbehind +# Lookahead y lookbehind (revisar delante/detrás) -Sometimes we need to find only those matches for a pattern that are followed or preceded by another pattern. +A veces necesitamos buscar únicamente aquellas coincidencias donde un patrón es precedido o seguido por otro patrón. -There's a special syntax for that, called "lookahead" and "lookbehind", together referred to as "lookaround". +Existe una sintaxis especial para eso llamadas "lookahead" y "lookbehind" ("ver delante" y "ver detrás"), juntas son conocidas como "lookaround" ("ver alrededor"). -For the start, let's find the price from the string like `subject:1 turkey costs 30€`. That is: a number, followed by `subject:€` sign. +Para empezar, busquemos el precio de la cadena siguiente `subject:1 pavo cuesta 30€`. Eso es: un número, seguido por el signo `subject:€`. ## Lookahead -The syntax is: `pattern:X(?=Y)`, it means "look for `pattern:X`, but match only if followed by `pattern:Y`". There may be any pattern instead of `pattern:X` and `pattern:Y`. +La sintaxis es: `pattern:X(?=Y)`. Esto significa "buscar `pattern:X`, pero considerarlo una coincidencia solo si es seguido por `pattern:Y`". Puede haber cualquier patrón en `pattern:X` y `pattern:Y`. -For an integer number followed by `subject:€`, the regexp will be `pattern:\d+(?=€)`: +Para un número entero seguido de `subject:€`, la expresión regular será `pattern:\d+(?=€)`: ```js run -let str = "1 turkey costs 30€"; +let str = "1 pavo cuesta 30€"; -alert( str.match(/\d+(?=€)/) ); // 30, the number 1 is ignored, as it's not followed by € +alert( str.match(/\d+(?=€)/) ); // 30, el número 1 es ignorado porque no está seguido de € ``` -Please note: the lookahead is merely a test, the contents of the parentheses `pattern:(?=...)` is not included in the result `match:30`. +Tenga en cuenta que "lookahead" es solamente una prueba, lo contenido en los paréntesis `pattern:(?=...)` no es incluido en el resultado `match:30`. -When we look for `pattern:X(?=Y)`, the regular expression engine finds `pattern:X` and then checks if there's `pattern:Y` immediately after it. If it's not so, then the potential match is skipped, and the search continues. +Cuando buscamos `pattern:X(?=Y)`, el motor de expresión regular encuentra `pattern:X` y luego verifica si existe `pattern:Y` inmediatamente después de él. Si no existe, entonces la coincidencia potencial es omitida y la búsqueda continúa. -More complex tests are possible, e.g. `pattern:X(?=Y)(?=Z)` means: +Es posible realizar pruebas más complejas, por ejemplo `pattern:X(?=Y)(?=Z)` significa: -1. Find `pattern:X`. -2. Check if `pattern:Y` is immediately after `pattern:X` (skip if isn't). -3. Check if `pattern:Z` is also immediately after `pattern:X` (skip if isn't). -4. If both tests passed, then the `pattern:X` is a match, otherwise continue searching. +1. Encuentra `pattern:X`. +2. Verifica si `pattern:Y` está inmediatamente después de `pattern:X` (omite si no es así). +3. Verifica si `pattern:Z` está también inmediatamente después de `pattern:X` (omite si no es así). +4. Si ambas verificaciones se cumplen, el `pattern:X` es una coincidencia. De lo contrario continúa buscando. -In other words, such pattern means that we're looking for `pattern:X` followed by `pattern:Y` and `pattern:Z` at the same time. +En otras palabras, dicho patrón significa que estamos buscando por `pattern:X` seguido de `pattern:Y` y `pattern:Z` al mismo tiempo. -That's only possible if patterns `pattern:Y` and `pattern:Z` aren't mutually exclusive. +Eso es posible solamente si los patrones `pattern:Y` y `pattern:Z` no se excluyen mutuamente. -For example, `pattern:\d+(?=\s)(?=.*30)` looks for `pattern:\d+` that is followed by a space `pattern:(?=\s)`, and there's `30` somewhere after it `pattern:(?=.*30)`: +Por ejemplo, `pattern:\d+(?=\s)(?=.*30)` busca un `pattern:\d+` que sea seguido por un espacio `pattern:(?=\s)` y que también tenga un `30` en algún lugar después de él `pattern:(?=.*30)`: ```js run -let str = "1 turkey costs 30€"; +let str = "1 pavo cuesta 30€"; alert( str.match(/\d+(?=\s)(?=.*30)/) ); // 1 ``` -In our string that exactly matches the number `1`. +En nuestra cadena eso coincide exactamente con el número `1`. -## Negative lookahead +## Lookahead negativo -Let's say that we want a quantity instead, not a price from the same string. That's a number `pattern:\d+`, NOT followed by `subject:€`. +Digamos que queremos una cantidad, no un precio de la misma cadena. Eso es el número `pattern:\d+` NO seguido por `subject:€`. -For that, a negative lookahead can be applied. +Para eso se puede aplicar un "lookahead negativo". -The syntax is: `pattern:X(?!Y)`, it means "search `pattern:X`, but only if not followed by `pattern:Y`". +La sintaxis es: `pattern:X(?!Y)`, que significa "busca `pattern:X`, pero solo si no es seguido por `pattern:Y`". ```js run -let str = "2 turkeys cost 60€"; +let str = "2 pavos cuestan 60€"; -alert( str.match(/\d+\b(?!€)/g) ); // 2 (the price is not matched) +alert( str.match(/\d+\b(?!€)/g) ); // 2 (el precio es omitido) ``` ## Lookbehind -Lookahead allows to add a condition for "what follows". +"lookahead" permite agregar una condición para "lo que sigue". -Lookbehind is similar, but it looks behind. That is, it allows to match a pattern only if there's something before it. +"Lookbehind" es similar. Permite coincidir un patrón solo si hay algo anterior a él. -The syntax is: -- Positive lookbehind: `pattern:(?<=Y)X`, matches `pattern:X`, but only if there's `pattern:Y` before it. -- Negative lookbehind: `pattern:(? pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy