blob: fd9222680de0a1c082e0b49e6d0fec66ca4c2bab (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
---
title: RegExp.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/RegExp/toString
tags:
- Expresion Regular
- Prototipo
- metodo
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/toString
original_slug: Web/JavaScript/Referencia/Objetos_globales/RegExp/toString
---
<div>{{JSRef}}</div>
<p>El método <strong><code>toString()</code></strong> devuelve una cadena que representa el patrón de la expresión regular.</p>
<h2 id="Sintaxis">Sintaxis</h2>
<pre class="syntaxbox"><code><var>regexObj</var>.toString();</code></pre>
<h3 id="Valor_de_Retorno">Valor de Retorno</h3>
<p>Una cadena que representa el objeto dado.</p>
<h2 id="Descripción">Descripción</h2>
<p>El objeto {{jsxref("RegExp")}} reemplaza el método <code>toString()</code> del objeto {{jsxref("Object")}}; no hereda de {{jsxref("Object.prototype.toString()")}}. Para objetos {{jsxref("RegExp")}}, el método <code>toString()</code> retorna una cadena que representa el patrón de la expresión regular.</p>
<h2 id="Ejemplos">Ejemplos</h2>
<h3 id="Usando_toString()">Usando <code>toString()</code></h3>
<p dir="ltr" id="tw-target-text">El siguiente ejemplo muestra la cadena de representación de un objeto {{jsxref("RegExp")}}:</p>
<pre class="brush: js">var myExp = new RegExp('a+b+c');
console.log(myExp.toString()); // '/a+b+c/'
var foo = new RegExp('bar', 'g');
console.log(foo.toString()); // '/bar/g'
</pre>
<h3 id="Expresiones_regulares_vacías_y_escapado">Expresiones regulares vacías y escapado</h3>
<p>A partir de ECMAScript 5, una expresión regular vacía devuelve la cadena "/(?:)/" y los terminadores de línea tales como "\n" son escapados:</p>
<pre class="brush: js">new RegExp().toString(); // "/(?:)/"
new RegExp('\n').toString() === "/\n/"; // true, antes de ES5
new RegExp('\n').toString() === "/\\n/"; // true, desde ES5
</pre>
<h2 id="Especificaciones">Especificaciones</h2>
<table class="standard-table" style="color: #3b3c40; font-size: 14px; font-weight: normal;">
<tbody>
<tr>
<th scope="col">Especificación</th>
<th scope="col">Estado</th>
<th scope="col">Comentario</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Definición inicial. Implementado en JavaScript 1.1.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.9.5.2', 'RegExp.prototype.toString')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Agregado de la definición para escapado de caracteres especiales y "(?:)" para expresiones regulares vacías.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-regexp.prototype.tostring', 'RegExp.prototype.toString')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-regexp.prototype.tostring', 'RegExp.prototype.toString')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilidad_en_Navegadores">Compatibilidad en Navegadores</h2>
{{Compat("javascript.builtins.RegExp.toString")}}
<h2 id="Vea_también">Vea también</h2>
<ul>
<li>{{jsxref("Object.prototype.toString()")}}</li>
</ul>
|