Tutoriales sobre RegExp



Eloquent JS

RegExp Objects

El Constructor

The RegExp constructor creates a regular expression object for matching text with a pattern.

Literal and constructor notations are possible:

new RegExp(pattern [, flags]);
  • 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.
  • When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary. For example, the following are equivalent:
var re = /\w+/;
var re = new RegExp("\\w+");
  • RegExp.prototype.exec

The exec() method executes a search for a match in a specified string. Returns a result array, or null.

If you are executing a match simply to find true or false, use the RegExp.prototype.test() method or the String.prototype.search() method.


If successful, search returns the index of the regular expression inside the string. Otherwise, it returns -1.

When you want to know whether a pattern is found in a string use search (similar to the regular expression test method); for more information (but slower execution) use match (similar to the regular expression exec method).


The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.
The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.

> re = /apples/gi
> str = "Apples are round, and apples are juicy."
'Apples are round, and apples are juicy.'
> newstr = str.replace(re, "oranges")
'oranges are round, and oranges are juicy.'

The replacement string can be a function to be invoked to create the new substring (to put in place of the substring received from parameter #1). The arguments supplied to this function are:

Possible name Supplied value
match The matched substring. (Corresponds to $&.)
p1, p2, ... The nth parenthesized submatch string, provided the first argument to replace was a RegExp object. (Corresponds to $1, $2, etc.) For example, if /(\a+)(\b+)/, was given, p1 is the match for \a+, and p2 for \b+.
offset The offset of the matched substring within the total string being examined (For example, if the total string was "abcd", and the matched substring was "bc", then this argument will be 1
string The total string being examined
Ejemplo de replace
[~/javascript/learning]$ pwd -P
[~/javascript/learning]$ cat f2c.js
#!/usr/bin/env node
function f2c(x)
  function convert(str, p1, offset, s)
    return ((p1-32) * 5/9) + "C";
  var s = String(x);
  var test = /(\d+(?:\.\d*)?)F\b/g;
  return s.replace(test, convert);

var arg = process.argv[2] || "32F";


[~/javascript/learning]$ ./f2c.js 100F
[~/javascript/learning]$ ./f2c.js 

Extensiones a las Expresiones Regulares

Expresiones Regulares Extendidas (a la Perl)
XRegExp instance properties
  • .xregexp.source (The original pattern provided to the XRegExp constructor)
  • .xregexp.flags (The original flags provided to the XRegExp constructor)
XRegExp. Unicode
Extensiones a las Expresiones Regulares en ECMA6
> console.log("\u03A0")
> console.log("\u03B1")
> "Πα".match(/\u03A0(\u03B1)/)
[ 'Πα', 'α', index: 0, input: 'Πα' ]

De EloquentJS:

By a strange historical accident, \s (whitespace) does not have this problem and matches all characters that the Unicode standard considers whitespace, including things like the nonbreaking space and the Mongolian vowel separator:

\s casa con el carácter unicode Mongolian Vowel


results matching ""

    No results matching ""