Getting Started with regular expressions and logging

Source: Internet
Author: User

Here you will find out what regular expressions are, some simple uses of regular expressions, and some regular expressions that are commonly used in your work, including the regular ones you use at work, for a rainy moment.

Introduction to a regular expression

  REGEXP is the abbreviation for regular expressions, when retrieving a text, you can use a pattern to describe what you want to retrieve. RegExp is the pattern .
a simple pattern can match one character. complex patterns include more characters and can parse, format check, replace, and so on. You can specify the location of the search in a string, the type of character to retrieve, and so on.

Suppose I have a string var str = "Test1213298adsadsad", so simply, the regular expression is the one that allows us to find a string in str and find the value of a feature.


notation: Normally two slash//is what we usually say, then add some value in//to the regular expression, such as/test/

two use of regular expressions
    • The definition and use of regular expressions
var New REGEXP ("Hello"var

There are two simple ways to declare a regular expression, and you can now use a regular rule to find the matching hello or world in a string.

Since we declare the regular is an object, then it is obvious that on such an object, there must be a number of ways to mount, the following to introduce these methods

    • The test () method--The test method retrieves the specified value from the string, and the return value is TRUE or False

   It will not continue to match until it finds the first matching value, and returns true directly.

var pat =/my/var str = "My name is Elephant ..."//
    • The exec () method-the Exec () method retrieves the specified value in a string. The return value is the value that was found. If no match is found, NULL is returned
var pat =/elephant/; Console.log (pat.exec (// return elephantvar pat1 =/ carlton/; Console.log (pat1.exec (// return null
    • The type of regular expression--/pattern/attributes

   Pattern is the value to match, and attributes is an optional string, and the common properties are ' g ', ' I ', respectively, for global matching and case-sensitive matching

// Case insensitivevarstr = "Visit Hunger"; varPATT1 =/hunger/i; Console.log (Str.match (PATT1));//Output Hunger//Global Matchvarstr= "Hello Hunger valley! I am Hunger "; varpatt1=/hunger/g; Console.log (Str.match (PATT1));//output [' Hunger ', ' hunger '] //Case insensitive , global matchvarstr= "Hello Hunger valley! I am Hunger "; varpatt1=/hunger/gi; Console.log (Str.match (PATT1));//output [' Hunger, ' Hunger ']
    • Search () Method--string lookup
var str= "Visit w3school!" ; Console.log (Str.search (/ ///
    • Match () method--string match
var str= "1 plus 2 equal"; Console.log (Str.match (/\d+/));   //  Console.log (Str.match (/\d+/g));  //
    • Replace () method--string substitution
var str= "I am hunger,i am Hunger" Console.log (str.replace (//str no change // "I am elephant, I am Hunger" // "I am elephant, I am Elephant"

  One thing to note here is that the Str.replace method only generates a temporary variable to output, and does not modify the value of STR

var str = "I am Hunger"; var temp = str.replace (/hunger/, elephant); // temp = "I am elephant", str = "I am Hunger"
    • Split () Method--string segmentation
var str = "Hello Hunger  ,  oh I am Hunger"; Str.split ("");   [' Hello ', ' Hunger ', ', ', ' Oh ', ' I ', ' am ', ' Hunger ']var temp =str.split (/\s+/); console.log (str); // "Hello Hunger, OH I am Hunger" // [' Hello ', ' Hunger ', ', ', ' Oh ', ' I ', ' am ', ' Hunger ']

The three-regular notation
    • [ABC] find any character between square brackets
var str= "Is this all there is?"  var patt1=/[a-h]///["H", "a", "H", "E", "E"]
    • [0-9] Find any number from 0 to 9
    • [A-z] find any characters from small to lowercase Z
    • [A-z] find any characters from uppercase A to uppercase Z
    • [A-z] find any characters from uppercase A to lowercase Z
    • [ADGK] finds any character within a given set
    • [^ADGK] finds any character outside a given set
    • Red|blue|green find any of the specified options
var str= "Hello hunger! How is it? "  var patt1=/hello|you///[' Hello ', ' you ']
    • Find a single character, except line breaks and line terminators

   In the h.t. Represents a character, and the regular rule is to find the corresponding string between H and T if there is a character connecting them together

var str= "That ' s hot!"  var patt1=/h.t///[' hat ', ' hot ']
    • \w Find Word characters (letters, numbers, underscores)
var str= "Give 100%!" ;   var // ["G", "I", "V", "E", "1", "0", "0"]
    • \w Find non-word characters
    • \d Finding numbers
    • \d finding non-numeric characters
    • \s Find white space characters (spaces, tab, line feeds, carriage returns)
    • \s finding non-whitespace characters
    • \b Matching non-word boundaries
    • \b Match word boundaries
/\bm/matches ' m ' in ' Moon ';/oo\b/does not match ' oo ' in ' moon ' because ' oo ' behind ' n '/oon\b/matches ' moon ' in ' oon ' because ' oon '  At the end of the string, there are no  word characters;var str= "Hello elephant";   var patt1=/\belephant///["Elephant"]
    • \ nto find line breaks
    • n+ matches any string that contains at least one n
var str= "Hello hhunger! Hello world! " ;   var patt1=/h+/G; document.write (Str.match (PATT1));   // [' H ', ' HH ', ' H ']
    • n matches any string containing 0 or more n
var str= "Hellooo hunger! Hello world! " ;   var patt1=/lo*/G; document.write (Str.match (PATT1)); // ["L", "looo", "L", "Lo", "L"]
    • N? Match any string that contains 0 or one n
var str= "1, or 1000?"  var patt1=/10?///[' 1 ', ' ten ', ' Ten ']
    • N{x} matches a string containing a sequence of X n
var str= "10000?  var// matches a string containing four digits //[' ' + ', ' + ']
    • N{x,y} matches a string containing a sequence of X or Y n
var str= "10000?  var patt1=/\d{3,4}/g;  // matches a string containing 3 digits or four digits // [' + ', ' + ', ' + ']
    • N{x,} matches a string containing at least X N of a sequence
var str= "10000?  var patt1=/\d{3,}/g;  // A string that contains at least 3 digits // [' + ', ' + ', ' 10000 ']
    • n$ matches any string ending with n
var str= "is this"; var patt1=/is$///[' is ']
    • ^n matches any string starting with N
var str= "Is this he"var patt1=/^is///[' is ']

Four Common regular
    • Kanji: [\U4E00-\U9FA5]
    • Phone Number: 1[0-9]{10}
    • E-mail: (\s) +[@]{1} (\s) +[.] {1} (\w) +
    • ID:/(^\d{15}$) | (^\d{17} ([0-9]|[ XX]) $)/---15 digits or 18 digits or 17 digits + 1 bit
    • Card number:/^[0-9]{15,16}$/--15 or 16 digits

Getting Started with regular expressions and logging

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.