RegExp in ECMAScript 262
Object representing a regular expression.
Inherits from:
Instance Properties
name | type | description |
---|---|---|
constructor | Object | A reference to the constructor class for the current object instance. [from Object] |
global | Boolean | (read only) The global flag for the regular expression. |
ignoreCase | Boolean | (read only) The case-insensitive flag for the regular expression. |
lastIndex | Number | The string position at which to start the next match. |
multiline | Boolean | (read only) The multiline flag for the regular expression. |
prototype | Object | The prototype for a class. [from Object] |
source | String | (read only) The regular expression expressed as a string. |
Instance Methods
name | returns | description |
---|---|---|
exec(sourceString) | Array | Run the regular expression against a string and return a single match. |
hasOwnProperty(propertyOrMethodName) | Boolean | Determines if the object/instance itself has the named property or method. [from Object] |
isPrototypeOf(instanceToTest) | Boolean | Determines if the calling object prototype is in the inheritance chain for the supplied argument. [from Object] |
propertyIsEnumerable(propertyOrMethodName) | Boolean |
Determines if the object/instance itself has a property or method of the supplied name which will appear in a for (prop in obj) enumeration.
[from Object]
|
test(sourceString) | Boolean |
Run the regular expression against a string; return true if a match exists, false otherwise.
|
toLocaleString() | String |
For most objects, the same as toString() unless explicitly overridden.
[from Object]
|
toString() | String | Returns a string representation of the object. [from Object] |
valueOf() | String |
Returns the internal this value of the object.
[from Object]
|
Description
A new RegExp
object can be created via three methods::
var foo = /pattern/flags; //regexp literal
var foo = new RegExp( patternAsString , flags ); //constructing from a string and flags
var foo = new RegExp( existingRegExp ); //duplicating the existing regular expression
The regexp literal notation (first method above) is usually faster than the latter, and should be used when possible. The pattern
for the regular expression, and the flags
(zero or more of the characters g
, i
and m
in any order) exist as literal text on the page with no other delimiters:
var oneCat = /cat/; //matches a single occurrence of "cat", case-sensitive
var anyCat = /cat/i; //matches a single occurence of "cat", case-insensitive
var all_cats = /cat/g; //matches every occurence of "cat", case-sensitive
var allCats = /cat/gi; //matches every occurence of "cat", case-insensitive
var oneInteger = /\d+/; //matches a single occurence one or more digits, 0-9
var allIntegers = /\d+/g; //matches every occurence of one or more digits
var theFirstLine = /^.+/g; //matches from the start of the string to the first newline
var everyLine = /^.+/gm; //matches from the start of a new line to end of that line
When the new RegExp(pattern, flags)
constructor is used, pattern
and flags
are both strings. (Though flags
may be omitted.)
var oneCat = new RegExp("cat");
var allCats = new RegExp("cat","gi");
The new RegExp()
constructor should primarily be used only when the full regular expression is not known during creation, but must be constructed from a string.
function KillHTMLTag(sourceString,htmlTagName,caseSensitive){
var flags = "g";
if (!caseSensitive) flags+="i";
var killRE = new RegExp("</?"+htmlTagName+"[^>]*>",flags);
return sourceString.replace(killRE,'');
}
var html = "<p>Hello <b>World</b></p>";
html = KillHTMLTag(html,'B');
// ** html is now "<p>Hello World</p>"
When using the new RegExp()
constructor to create a regular expression from a string, remember that both string escaping and special regular expression escaping are needed:
var oneInteger = /\d+/;
var oneInteger = new RegExp("\\d+");
var findDogBackslashCat = /Dog\\Cat/;
var findDogBackslashCat = new RegExp("Dog\\\\Cat"); //Extra \ needed in string form
A full discussion of regular expression syntax is beyond the scope of this reference. The MSDN Introduction to Regular Expressions may be helpful to the beginner, especially the Regular Expression Syntax section.
Arguments that are a RegExp
name | in method | of object | description |
---|---|---|---|
expr | match(expr) | String | The regular expression to matc |
searchExpr | search(searchExpr) | String | The regular expression to sear |