Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

9-14 lookaround tareas -atado otro pr -resolver #385 primero- x temas de glosario#490

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
joaquinelio merged 5 commits intojavascript-tutorial:masterfromjoaquinelio:taka
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@

The regexp for an integer number is `pattern:\d+`.
La expresión regular para un número entero es `pattern:\d+`.

We can exclude negatives by prepending it with the negativelookbehind: `pattern:(?<!-)\d+`.
Podemos excluir los negativos anteponiendo un "lookbehind negativo": `pattern:(?<!-)\d+`.

Although, if we try it now, we may notice one more "extra" result:
Pero al probarlo, notamos un resultado de más:

```js run
let regexp = /(?<!-)\d+/g;
Expand All@@ -13,11 +13,11 @@ let str = "0 12 -5 123 -18";
console.log( str.match(regexp) ); // 0, 12, 123, *!*8*/!*
```

As you can see, it matches`match:8`,from `subject:-18`.To exclude it, we need to ensure that theregexp starts matching a number not from the middle of another (non-matching) number.
Como puedes ver, hay coincidencia de`match:8`,con `subject:-18`.Para excluirla necesitamos asegurarnos de que `regexp` no comience la búsqueda desde el medio de otro número (no coincidente).

We can do it by specifying another negativelookbehind: `pattern:(?<!-)(?<!\d)\d+`.Now `pattern:(?<!\d)`ensures that a match does not start after another digit, just what we need.
Podemos hacerlo especificando otra precedencia "lookbehind negativo": `pattern:(?<!-)(?<!\d)\d+`.Ahora `pattern:(?<!\d)`asegura que la coicidencia no comienza después de otro dígito, justo lo que necesitamos.

We can also join them into a singlelookbehind here:
También podemos unirlos en un único "lookbehind":

```js run
let regexp = /(?<![-\d])\d+/g;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
#Find non-negative integers
#Encontrar enteros no negativos

There's a stringof integer numbers.
Tenemos un stringde números enteros.

Create a regexp that looks for only non-negative ones (zero is allowed).
Crea una expresión regular que encuentre solamente los no negativos (el cero está permitido).

An example of use:
Un ejemplo de uso:
```js
let regexp = /your regexp/g;
let regexp = /tu regexp/g;

let str = "0 12 -5 123 -18";

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
In order to insert after the`<body>` tag, we must first find it. We can use the regular expression pattern`pattern:<body.*?>` for that.
Para insertar algo después de la etiqueta`<body>`, primero debemos encontrarla. Para ello podemos usar la expresión regular`pattern:<body.*?>`.

In this task we don't need to modify the`<body>` tag. We only need to add the text after it.
En esta tarea no debemos modificar la etiqueta`<body>`. Solamente agregar texto después de ella.

Here's how we can do it:
Veamos cómo podemos hacerlo:

```js run
let str = '...<body style="...">...';
Expand All@@ -11,9 +11,9 @@ str = str.replace(/<body.*?>/, '$&<h1>Hello</h1>');
alert(str); // ...<body style="..."><h1>Hello</h1>...
```

In the replacementstring `$&`means the match itself, that is, the part of the source text that corresponds to`pattern:<body.*?>`.It gets replaced by itself plus `<h1>Hello</h1>`.
En elstringde reemplazo,`$&`significa la coincidencia misma, la parte del texto original que corresponde a`pattern:<body.*?>`.Es reemplazada por sí misma más `<h1>Hello</h1>`.

An alternative is to uselookbehind:
Una alternativa es el uso de "lookbehind":

```js run
let str = '...<body style="...">...';
Expand All@@ -22,15 +22,15 @@ str = str.replace(/(?<=<body.*?>)/, `<h1>Hello</h1>`);
alert(str); // ...<body style="..."><h1>Hello</h1>...
```

As you can see, there's onlylookbehind part in this regexp.
Como puedes ver, solo está presente la parte "lookbehind" en esta expresión regular.

It works like this:
-At every position in the text.
-Check if it's preceeded by `pattern:<body.*?>`.
-If it's so then we have the match.
Esto funciona así:
-En cada posición en el texto.
-Chequea si está precedida por `pattern:<body.*?>`.
-Si es así, tenemos una coincidencia.

The tag `pattern:<body.*?>`won't be returned. The result of this regexp is literally an emptystring, but it matches only at positions preceeded by `pattern:<body.*?>`.
La etiqueta `pattern:<body.*?>`no será devuelta. El resultado de esta expresión regular es unstring vacío, pero coincide solo en las posiciones precedidas por `pattern:<body.*?>`.

So it replaces the "empty line",preceeded by `pattern:<body.*?>`,with `<h1>Hello</h1>`.That's the insertion after `<body>`.
Entonces reemplaza la "linea vacía",precedida por `pattern:<body.*?>`,con `<h1>Hello</h1>`.Esto es, la inserción después de `<body>`.

P.S.Regexp flags, such as`pattern:s`and `pattern:i`can also be useful: `pattern:/<body.*?>/si`.The`pattern:s`flag makes the dot`pattern:.`match a newline character, and`pattern:i`flag makes `pattern:<body>`also match`match:<BODY>`case-insensitively.
P.S.Los indicadores de Regexp tales como`pattern:s`y `pattern:i`también nos pueden ser útiles: `pattern:/<body.*?>/si`.El indicador`pattern:s`hace que que el punto`pattern:.`coincida también con el carácter de salto de línea, y el indicador`pattern:i`hace que `pattern:<body>`también acepte coincidencias`match:<BODY>`en mayúsculas y minúsculas.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
#Insert After Head
#Insertar después de la cabecera

We have astringwith an HTML Document.
Tenemos unstringcon un documento HTML.

Write aregularexpression that inserts`<h1>Hello</h1>`immediately after`<body>` tag. The tag may have attributes.
Escribe una expresiónregularque inserte`<h1>Hello</h1>`inmediatamente después de la etiqueta`<body>`. La etiqueta puede tener atributos.

For instance:
Por ejemplo:

```js
let regexp = /your regular expression/;
let regexp = /tu expresión regular/;

let str = `
<html>
Expand All@@ -20,7 +20,7 @@ let str = `
str = str.replace(regexp, `<h1>Hello</h1>`);
```

After that the value of`str`should be:
Después de esto el valor de`str`debe ser:
```html
<html>
<body style="height: 200px"><h1>Hello</h1>
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp