GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialrepublic.com — Regular Expressions in JavaScript - Tutorial Republic JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output
JS Data Types
JS Operators
JS Events
JS Strings
JS Numbers
JS If…Else
JS Switch…Case
JS Arrays
JS Sorting Arrays
JS Loops
JS Functions
JS Objects
JAVASCRIPT & DOM
JS DOM Nodes
JS DOM Selectors
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation
JS DOM Navigation
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JS Date and Time
JS Math Operations
JS Type Conversions
JS Event Listeners
JS Event Propagation
JS Borrowing Methods
JS Hoisting Behavior
JS Closures
JS Strict Mode
JS JSON Parsing
JS Error Handling
JS Regular Expressions
JS Form Validation
JS Cookies
JS AJAX Requests
JS ES6 Features
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE
Advertisements
JavaScript Regular ExpressionsIn this tutorial you will learn how regular expressions work, as well as how to use them to perform pattern matching in an efficient way in JavaScript. What is Regular ExpressionRegular Expressions, commonly known as "regex" or "RegExp", are a specially formatted text strings used to find patterns in text. Regular expressions are one of the most powerful tools available today for effective and efficient text processing and manipulations. For example, it can be used to verify whether the format of data i.e. name, email, phone number, etc. entered by the user is correct or not, find or replace matching string within text content, and so on. JavaScript supports Perl style regular expressions. Why Perl style regular expressions? Because Perl (Practical Extraction and Report Language) was the first mainstream programming language that provided integrated support for regular expressions and it is well known for its strong support of regular expressions and its extraordinary text processing and manipulation capabilities. Let's begin with a brief overview of the commonly used JavaScript's built-in methods for performing pattern-matching before delving deep into the world of regular expressions.
Note: The methods Defining Regular ExpressionsIn JavaScript, regular expressions are represented by RegExp object, which is a native JavaScript object like String, Array, and so on. There are two ways of creating a new RegExp object — one is using the literal syntax, and the other is using the The literal syntax uses forward slashes ( ExampleRun this code »
As you can see, the regular expression literal syntax is shorter and easier to read. Therefore it is preferable to use the literal syntax. We'll also use it throughout this tutorial. Note: When using the constructor syntax, you've to double-escape special characters, which means to match "." you need to write Pattern Matching with Regular ExpressionRegular expression patterns include the use of letters, digits, punctuation marks, etc., plus a set of special regular expression characters (do not confuse with the HTML special characters). The characters that are given special meaning within a regular expression, are: The following sections describe the various options available for formulating patterns: Character ClassesSquare brackets surrounding a pattern of characters are called a character class e.g. Negated character classes can also be defined that match any character except those contained within the brackets. A negated character class is defined by placing a caret ( You can also define a range of characters by using the hyphen (
The following example will show you how to find whether a pattern exists within a string or not using the regular expression with the JavaScript ExampleRun this code »
Further, you can add the global flag ExampleRun this code »
Tip: Regular expressions aren't exclusive to JavaScript. Languages such as Java, Perl, Python, PHP, etc. use the same notation for finding patterns in text. Predefined Character ClassesSome character classes such as digits, letters, and whitespaces are used so frequently that there are shortcut names for them. The following table lists those predefined character classes:
The following example will show you how to find and replace space with a hyphen character in a string using regular expression with the JavaScript ExampleRun this code »
Repetition QuantifiersIn the previous section we've learnt how to match a single character in a variety of fashions. But what if you want to match on more than one character? For example, let's say you want to find out words containing one or more instances of the letter p, or words containing at least two p's, and so on. This is where quantifiers come into play. With quantifiers you can specify how many times a character in a regular expression should match. Quantifiers can be applied to the individual characters, as well as classes of characters, and groups of characters contained by the parentheses. The following table lists the various ways to quantify a particular pattern:
The regular expression in the following example will splits the string at comma, sequence of commas, whitespace, or combination thereof using the JavaScript ExampleRun this code »
Position AnchorsThere are certain situations where you want to match at the beginning or end of a line, word, or string. To do this you can use anchors. Two common anchors are caret (
The regular expression in the following example will match only those names in the names array which start with the letter "J" using the JavaScript ExampleRun this code »
Pattern Modifiers (Flags)A pattern modifier allows you to control the way a pattern match is handled. Pattern modifiers are placed directly after the regular expression, for example, if you want to search for a pattern in a case-insensitive manner, you can use the The following table lists some of the most commonly used pattern modifiers.
The following example will show you how to use the ExampleRun this code »
Similarly, the following example shows how to match at the beginning of every line in a multi-line string using the ExampleRun this code »
AlternationAlternation allows you to specify alternative version of a pattern. Alternation in a regular expression works just like the You can specify alternation using a vertical bar ( ExampleRun this code »
Note: Alternatives are evaluated from left to right until a match is found. If the left alternative matches, the right alternative is ignored completely even if it has a match. GroupingRegular expressions use parentheses to group subexpressions, just like mathematical expressions. Parentheses allow a repetition quantifier to be applied to an entire subexpression. For example, in regexp ExampleRun this code »
Note: If the string matches the pattern, the Tip: If the regular expression includes the Word BoundariesA word boundary character ( Similarly, the regexp The following example will highlight the words beginning with car in bold: ExampleRun this code »
Advertisements
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |