JavaScript Regular Expressions (highly recommended) (1/6)

Source: Internet
Author: User
Tags regular expression

A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters). This pattern describes one or more strings to be matched when looking for a text body. A regular expression is used as a template to match a character pattern with the string being searched for.
Creating regular Expressions
The Www.111cn.net code is as follows:
var re = new RegExp ();//regexp is an object, just like Aarray
But without any effect, you need to pass the contents of the regular expression as a string.
Re =new regexp ("a");/The simplest regular expression will match the letter A
Re=new regexp ("A", "I");//The second parameter, which indicates that the match is not case-insensitive

The first parameter of the RegExp constructor is the text content of the regular expression, and the first argument is an optional flag. Flags can be combined with
g (Full-text search)
I. (Ignore case)
m (Multiple-line lookup)
The Www.111cn.net code is as follows:
var re = new RegExp ("A", "GI");//Match all A or a

The regular expression also has another way of declaring the literal amount of the regular expression
The Www.111cn.net code is as follows:
var re =/a/gi;
Methods and properties related to regular expressions
Methods of regular Expression objects
test, returns a Boolean value that indicates whether the pattern exists in the string being searched. Returns true if present, otherwise returns false.
exec, runs the lookup in the string with the regular expression pattern and returns the package <script type= "text/Web page Special effects" src= "http://www.javaeye.com/javascripts/tinymce/themes/ Advanced/langs/zh.js "></script><script type=" Text/javascript "src=" http://www.javaeye.com/ Javascripts/tinymce/plugins/javaeye/langs/zh.js "></script> An array containing the results of the lookup.
compile, the regular expression is compiled into an internal format, which executes faster.
The properties of the regular Expression object
source, returns a copy of the text of the regular expression pattern. Read-only.
lastindex, returns the character position, which is the starting position of the next successful match in the lookup string.
$1...$9, returns nine of the most recently saved parts found during pattern matching. Read-only.
input ($_), returns a string that performs the lookup of the canonical representation. Read-only.
Lastmatch ($&) that returns the last-matched character in any regular expression search. Read-only.
Lastparen ($+), if any, returns the last child match in any regular expression lookup. Read-only.
leftcontext ($ ') returns the character found between the position from the beginning of the string to the last match in the string being searched. Read-only.
rightcontext ($ ') returns the character from the last match position to the end of the string in the searched string. Read-only.
String objects some methods related to regular expressions
match, finds a match for one or more regular expressions.
Replace, replacing the substring that matches the regular expression.
Search, retrieves a value that matches the regular expression.
split, splits the string into an array of strings.
Test how regular expressions work!
The Www.111cn.net code is as follows:
The test method, which tests the string, returns True when it conforms to the pattern, or returns false
var re =/he/;//simplest regular expression that matches the word he
var str = "he";
Alert (Re.test (str));//true
str = "we";
Alert (Re.test (str));//false
str = "he";
Alert (Re.test (str));//false, uppercase, if you want to match case, you can specify I flag (i is ignorecase or case-insensitive representation)
re =/he/i;
Alert (Re.test (str));//true
str = "Certainly!he loves her!";
Alert (Re.test (str));//true, as long as the inclusion of he is in accordance with, if you want to just he or he, cannot have other characters, you can use ^ and $
Re =/^he/i;//character (^) represents the start position of a character
Alert (Re.test (str));//false, because he was not at the beginning of str
str = "He is a good boy!";
Alert (Re.test (str));//true,he is the character start position, and you need to use the $
Re =/^he$/i;//$ indicates the end position of the character
Alert (Re.test (str));//false
str = "he";
Alert (Re.test (str));//true
Of course, this does not reveal how powerful the regular expression is, because we can use = = or indexof in the example above
Re =/s/;//s matches any white space character, including spaces, tabs, page breaks, and so on
str= "user name";//username contains spaces
Alert (Re.test (str));//true
str = "user name";//user name contains tab
Alert (Re.test (str));//true
Re=/^[a-z]/i;//[] matches any character within a specified range, where the English alphabet is matched, case-insensitive
Str= "variablename";//variable name must begin with a letter
Alert (Re.test (str));//true
Str= "123ABC";
Alert (Re.test (str));//false

Home 1 2 3 4 5 6 last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.