Sumario
Crea un objeto 'expresión regular' para encontrar texto de acuerdo a un patrón.
Sintaxis
RegExp(patrón [, flags])
/patrón/flags
Parametros
-
patrón
- El texto de la expresión regular.
-
flags
Si se especifica, los flags puenden ser una combinación de los siguientes valores:
-
g
- busqueda global (global match)
-
i
- ignorar mayúsculas o minúsculas
-
m
- Tratar caracteres de inicio y fin (^ y $) como multiples líneas de texto(por ejemplo: encontrar el inicio o fin de cada línea delimitada por \n o \r, no sólo al inicio o fin de toda la entrada de texto)
-
y
-
sticky; matches only from the index indicated by thelastIndex
property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of thelastIndex
property.
Descripción
Cuando se usa la función constructor, la cadena de escape de reglas normal (precedida del caracter especial \ cuando incluye un string) es necesaria. Por ejemplo, lo siguiente es equivalente:
var re = new RegExp("\\w+"); var re = /\w+/;
Hay que remarcar que los parámetros en formato literal no usan comillas para indicar strings, mientras que los parámetros para la funcion constructor usan comillas. Así la siguiente expresión crea la misma expresión regular:
/ab+c/i; new RegExp("ab+c", "i");
Caracteres especiales en expresiones regulares
Carácter | Significado |
\ |
Para los caracteres que generalmente son tratados literalmente, indica que el siguiente carácter es especial y no debe interpretarse literalmente. Por ejemplo, o Para los caracteres que normalmente se tratan de forma especial, indica que el siguiente carácter no es especial y debe ser interpretado literalmente. Por ejemplo, * es un carácter especial que significa que 0 o más ocurrencias del carácter precedente deberían ser encontradas; por ejemplo, |
^ |
Encuentra el principio de la entrada. Si el flag multilínea aparece como verdadero, también coincide con el carácter inmediatamente después de un carácter de salto de línea. Por ejemplo, |
$ |
Encuentra el final de la entrada. Si el flag multilínea es puesto a verdadero, también encuentra el carácter inmediatamente después al carácter de salto de linea. Por ejemplo, |
* |
Encuentra el elemento anterior 0 o más veces. Por ejemplo, |
+ |
Encuentra el elemento precedente 1 o mas veces. Es equivalente a Por ejemplo, |
? |
Encuentra el elemento precedente 0 o una vez. Por ejemplo, Si es usado inmediantamente despues de cualquier cuantificador También usado en las afirmaciones posteriores, descritas bajo |
. |
(El punto decimal) encuentra cualquier carácter excepto los caracteres de nueva linea: \n \r \u2028 o \u2029. ( Por ejemplo, |
(x) |
Encuentra Por ejemplo, |
(?:x) |
Encuentra |
x(?=y) |
Encuentra |
x(?!y) |
Encuentra
|
x|y |
Coincide con Por ejemplo, |
{n} |
Cuando Por ejemplo, |
{n,} |
Donde Por ejemplo, |
{n,m} |
Donde Por ejemplo, |
[xyz] |
Un set de caracteres. Encuentra cualquiera de ellos. Puedes espedificar el rango de caracteres usando un guion (-). Por ejemplo, |
[^xyz] |
A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets. You can specify a range of characters by using a hyphen. For example, |
[\b] |
Matches a backspace. (Not to be confused with |
\b |
Matches a word boundary, such as a space. (Not to be confused with For example, |
\B |
Matches a non-word boundary. For example, |
\cX |
Where For example, |
\d |
Matches a digit character in the basic Latin alphabet. Equivalent to Note: In Firefox 2 and earlier, matches a digit character from any alphabet. (bug 378738) For example, |
\D |
Matches any non-digit character in the basic Latin alphabet. Equivalent to Note: In Firefox 2 and earlier, all alphabet. (bug 378738) For example, |
\f |
Matches a form-feed. |
\n |
Matches a linefeed. |
\r |
Matches a carriage return. |
\s |
Matches a single white space character, including space, tab, form feed, line feed and other unicode spaces.[equivalent_s] For example, |
\S |
Matches a single character other than white space.[equivalent_S] For example, |
\t |
Matches a tab. |
\v |
Matches a vertical tab. |
\w |
Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to For example, |
\W |
Matches any character that is not a word character from the basic Latin alphabet. Equivalent to For example, |
\n |
Where For example, |
\0 |
Matches a NUL character. Do not follow this with another digit. |
\xhh |
Matches the character with the code |
\uhhhh |
Matches the character with the Unicode value |
The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.
The constructor of the regular expression object, for example, new RegExp("ab+c")
, provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
- ^Equivalent to:
[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
- ^Equivalent to:
[^\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
Properties
For properties available on RegExp
instances, see Properties of RegExp instances.
- prototype
- Allows the addition of properties to all objects.
Methods
For methods available on RegExp
instances, see Methods of RegExp instances.
The global RegExp
object has no methods of its own, however, it does inherit some methods through the prototype chain.
Properties
See also deprecated RegExp
properties.
Note that several of the RegExp
properties have both long and short (Perl-like) names. Both names always refer to the same value. Perl is the programming language from which JavaScript modeled its regular expressions.
RegExp.prototype.constructor
- Specifies the function that creates an object's prototype.
RegExp.prototype.flags
- A string that contains the flags of the
RegExp
object. RegExp.prototype.global
- Whether to test the regular expression against all possible matches in a string, or only against the first.
RegExp.prototype.ignoreCase
- Whether to ignore case while attempting a match in a string.
RegExp.prototype.multiline
- Whether or not to search in strings across multiple lines.
RegExp.prototype.source
- The text of the pattern.
RegExp.prototype.sticky
- Whether or not the search is sticky.
RegExp.prototype.unicode
- Whether or not Unicode features are enabled.
Methods
RegExp.prototype.compile()
- (Re-)compiles a regular expression during execution of a script.
RegExp.prototype.exec()
- Executes a search for a match in its string parameter.
RegExp.prototype.test()
- Tests for a match in its string parameter.
RegExp.prototype.toSource()
- Returns an object literal representing the specified object; you can use this value to create a new object. Overrides the
Object.prototype.toSource()
method. RegExp.prototype.toString()
- Returns a string representing the specified object. Overrides the
Object.prototype.toString()
method.
Properties
See also deprecated RegExp
properties.
Note that several of the RegExp
properties have both long and short (Perl-like) names. Both names always refer to the same value. Perl is the programming language from which JavaScript modeled its regular expressions.
RegExp.prototype.constructor
- Specifies the function that creates an object's prototype.
RegExp.prototype.flags
- A string that contains the flags of the
RegExp
object. RegExp.prototype.global
- Whether to test the regular expression against all possible matches in a string, or only against the first.
RegExp.prototype.ignoreCase
- Whether to ignore case while attempting a match in a string.
RegExp.prototype.multiline
- Whether or not to search in strings across multiple lines.
RegExp.prototype.source
- The text of the pattern.
RegExp.prototype.sticky
- Whether or not the search is sticky.
RegExp.prototype.unicode
- Whether or not Unicode features are enabled.
Methods
RegExp.prototype.compile()
- (Re-)compiles a regular expression during execution of a script.
RegExp.prototype.exec()
- Executes a search for a match in its string parameter.
RegExp.prototype.test()
- Tests for a match in its string parameter.
RegExp.prototype.toSource()
- Returns an object literal representing the specified object; you can use this value to create a new object. Overrides the
Object.prototype.toSource()
method. RegExp.prototype.toString()
- Returns a string representing the specified object. Overrides the
Object.prototype.toString()
method.
Examples
Example: Using a regular expression to change data format
The following script uses the replace method inherited by the String instance to match a name in the format first last and output it in the format last, first. In the replacement text, the script uses $1
and $2
to indicate the results of the corresponding matching parentheses in the regular expression pattern.
var re = /(\w+)\s(\w+)/; var str = "John Smith"; var newstr = str.replace(re, "$2, $1"); print(newstr);
This displays "Smith, John".
Example: Using a regular expression with the "sticky" flag
This example demonstrates how one could use the sticky flag on regular expressions to match individual lines of multiline input.
var text = "First line\nsecond line"; var regex = /(\S+) line\n?/y; var match = regex.exec(text); print(match[1]); // prints "First" print(regex.lastIndex); // prints 11 var match2 = regex.exec(text); print(match2[1]); // prints "Second" print(regex.lastIndex); // prints "22" var match3 = regex.exec(text); print(match3 === null); // prints "true"
One can test at run-time whether the sticky flag is supported, using try { … } catch { … }
. For this, either an eval(…)
expression or the RegExp(regex-string, flags-string)
syntax must be used (since the /regex/flags
notation is processed at compile-time, so throws an exception before the catch
block is encountered). For example:
var supports_sticky; try { RegExp('','y'); supports_sticky = true; } catch(e) { supports_sticky = false; } alert(supports_sticky); // alerts "false" in Firefox 2, "true" in Firefox 3+
See also
- Regular Expressions chapter in the JavaScript Guide