JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
The "RegExp" Object Type - Regular Expression Patterns
This section provides a quick description and a tutorial example script on the 'RegExp' built-in object type, which is used to create 'RegExp' objects for pattern matching operations.
The "RegExp" object type is a built-in object type to be used to create a "RegExp" object, representing a regular expression pattern. It has the following main features:
Here is a tutorial example script showing some of those features:
// RegExp_Object_Type.js // Copyright (c) 2013 HerongYang.com. All Rights Reserved. // Creating a new RegExp object var myObject = new RegExp('\\S+@.*\.com','i'); // Checking this object println("\nAbout this \"myObject\":"); println(" Type = "+(typeof myObject)); println(" Instance Of Object: "+(myObject instanceof Object)); println(" Instance Of RegExp: "+(myObject instanceof RegExp)); println("\nProperties of this \"myObject\":"); println(" myObject.source = "+myObject.source); println(" myObject.global = "+myObject.global); println(" myObject.ignoreCase = "+myObject.ignoreCase); println(" myObject.multiline = "+myObject.multiline); println(" myObject.lastIndex = "+myObject.lastIndex); // Using RegExp's inherited methods var text = "Please write to me@herongyang.com for help."; var t = myObject.test(text); var e = myObject.exec(text); println("\nExecution result:"); println(" typeof myObject = "+(typeof myObject)); println(" typeof myObject.test() = "+(typeof t)); println(" myObject.toString() = "+myObject.toString()); println(" myObject.test() = "+t); println("\nAbout myObject.exec():"); println(" typeof myObject.exec() = "+(typeof e)); println(" myObject.exec() instanceOf Array: "+(e instanceof Array)); for (var item in e) { println(" "+item+" = "+e[item]); }
If you run this script with "jrunscript", you will get:
About this "myObject": Type = function Instance Of Object: true Instance Of RegExp: true Properties of this "myObject": myObject.source = \S+@.*.com myObject.global = false myObject.ignoreCase = true myObject.multiline = false myObject.lastIndex = 0 Execution result: typeof myObject = function typeof myObject.test() = boolean myObject.toString() = /\S+@.*.com/i myObject.test() = true About myObject.exec(): typeof myObject.exec() = object myObject.exec() instanceOf Array: true 0 = me@herongyang.com index = 16 input = Please write to me@herongyang.com for help.
Now we know how to create "RegExp" objects with the "RegExp()" constructor. The output also shows that the "typeof myObject" returns "function". Should it be "object"?
Read other "RegExp" related chapters to see more tutorial examples.
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
Defining Your Own Object Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
Using Functions as "Function" Objects
►Introduction to Built-in Object Types
Overview of Built-in Object Types
The "Object" Object Type - The Root Object Type
The "Global" Object Type - The Invisible Global Container
Global Properties and Functions Defined in ECMAScript
Global Properties and Functions Provided by "jrunscript"
The "Function" Object Type - Functions Are Objects
The "Array" Object Type - Arrays Are Objects
The "JSON" Object Type - parse() and stringify()
The "String" Object Type - Not Equal to String Primitive Type
The "Boolean" Object Type - Wrapping Boolean Values into Objects
The "Number" Object Type - Not Equal to Number Primitive Type
The "Date" Object Type - Managing Dates and Times
►The "RegExp" Object Type - Regular Expression Patterns
The "Error" Object Type - Runtime Exceptions
The "Math" Object Type - The Math Container
W3C's Document Object Model (DOM) Specifications