Common functions of js Regular Expressions (continued ),

Source: Internet
Author: User

Common functions of js Regular Expressions (continued ),

Regular Expression object Method

1,Test,Returns a Boolean value indicating whether the pattern exists in the searched string. If yes, true is returned. Otherwise, false is returned.
2,Exec,Run the search in the string in regular expression mode and return an array containing the search result.
3,Compile,Compile the regular expression into an internal format for faster execution.
Attributes of a regular expression object

1,Source,Returns a copy of the text in regular expression mode. Read-only.
2,LastIndex,Returns the starting position of the next successful match in the string to be searched.
3,Input ($ _),Returns the query string in the execution specification. Read-only.
4,LastMatch ($ &),Returns the final matched characters in any regular expression search. Read-only.
5,LastParen ($ + ),If any, return the child matching in the final part of any regular expression. Read-only.
6,LeftContext ($ '),Returns the characters in the searched string from the starting position of the string to the position before the final match. Read-only.
7,RightContext ($ '),Returns the character from the last matching position to the end of the string. Read-only.
Some methods related to the regular expression of the String object

1,Match,Find the matching of one or more regular expressions.
2,RePlace,Replace the substring that matches the regular expression.
3,Search,Returns the value that matches the regular expression.
4,Split,Splits a string into a string array.
Case 1Test Method

// Test method, test string. true is returned when the mode is met. Otherwise, false var re =/he/is returned. // The simplest regular expression, match the word he var str = "he"; console. log (re. test (str); // true str = "we"; console. log (re. test (str); // false str = "HE"; console. log (re. test (str); // false, in upper case. You can specify the I flag (I is an expression of ignoreCase or case-insensitive) if both cases match. re =/he/I; console. log (re. test (str); // true str = "Certainly! He loves her! "; Console. log (re. test (str); // true, as long as it contains he (HE), it will match. If it is only he or HE, there cannot be other characters, you can use ^ and $ re =/^ he/I; // escape character (^) to represent the start position of the character console. log (re. test (str); // false, because he is not at the beginning of str = "He is a good boy! "; Console. log (re. test (str); // true, He is the character start position, and $ re =/^ he $/I; // $ indicates the character end position console. log (re. test (str); // false str = "He"; console. log (re. test (str); // true // Of Course, you cannot find how powerful the regular expression is, because we can use = or indexOf re =/\ s/; // \ s to match any blank characters in the preceding example, str = "user Name", including spaces, tabs, and page breaks; // the user Name contains space console. log (re. test (str); // true str = "user Name"; // the user Name contains the tab console. log (re. test (str); // true re =/^ [a-z]/I; // [] matches any character in the specified range. Here, the English letter is matched, case-insensitive str = "variableName"; // The variable name must start with a letter. log (re. test (str); // true str = "123abc"; console. log (re. test (str); // false

Case 2Exec Test

Var haoVersion = "Haorooms 8"; // The value 8 indicates the system master version. var re =/^ [a-z] + \ s + \ d + $/I; // + indicates that the character must appear at least once, \ s indicates a blank character, and \ d indicates a digital console. log (re. test (haoVersion); // true, but we want to know the main version number // another method exec, returns an array, the first element of the array is the complete Matching content re =/^ [a-z] + \ s + \ d + $/I; arr = re.exe c (haoVersion); console. log (arr [0]); // outputs the complete haoVersion, because the entire string exactly matches the re // I only need to retrieve the number re =/\ d + /; var arr = re.exe c (haoVersion); console. log (arr [0]); // 8 // The array 1st to nelement returned by exec contains any child matching in the match. re =/^ [a-z] + \ s + (\ d +) $/I; // use () to create a sub-match arr named re.exe c (haoVersion); console. log (arr [0]); // The entire haoVersion, that is, the complete matching console of the regular expression. log (arr [1]); // 8, the first child matches, the fact can also be taken out of the master version console. log (arr. length); // 2 haoVersion = "Haorooms 8.10"; // obtain the master and secondary versions. re =/^ [a-z] + \ s + (\ d +) \. (\ d +) $/I ;//. is one of the metacharacters of the regular expression. to use its literal meaning, you must escape arr = re.exe c (haoVersion); console. log (arr [0]); // complete haoVersion console. log (arr [1]); // 8 console. log (arr [2]); // 10

Case 3 some methods of String objects related to regular expressions

1. About replace, I wrote a blog dedicated to it. You can also pass parameters.

2. Other operations

// Replace method, used to replace the string var str = "some money"; console. log (str. replace ("some", "much"); // The first parameter of much money // replace can be the regular expression var re =/\ s /; // blank character console. log (str. replace (re, "%"); // some % money // when you do not know how many white spaces are in the string, regular Expressions are extremely convenient for str = "some \ tsome \ t \ f"; re =/\ s +/; console. log (str. replace (re, "#"); // but this will only replace a bunch of blank characters that appear for the first time. // because a regular expression can only match once, \ s + matches the first space and then exits re =/\ s +/g; // g, global flag. The regular expression matches the entire string console. log (st R. replace (re, "@"); // some @ // another similarity is split var str = "a-bd-c "; var arr = str. split ("-"); // Returns ["a", "bd", "c"] // If str is input by the user, he may input a-bd-c or a bd c or a_bd_c, but it won't be abdc (so that he is wrong) str = "a_db-c "; // The user adds the separator s re =/[^ a-z]/I in his favorite way; // we have mentioned ^ to indicate that the character starts, however, in [], it indicates a negative character set // It matches any character that is not within the specified range. Here, it matches all characters except letters, arr = str. split (re); // still Returns ["a", "bd", "c"]; // indexOf, search str = "My age is 18. golden Age! "; // The age is not certain. We cannot use indexOf to find its location re =/\ d +/; console. log (str. search (re); // returns the searched string starting with subscript 10 // note, because the search itself is the first occurrence and returns immediately, therefore, you do not need to use the g flag when searching. // although the following code does not make an error, the g flag is redundant re =/\ d +/g; console. log (str. search (re); // still 10 var str = "My name is CJ. hello everyone! "; Var re =/[A-Z]/; // match all uppercase letters var arr = str. match (re); // returns the array console. log (arr); // The array contains only one M, because we didn't use global match re =/[A-Z]/g; arr = str. match (re); console. log (arr); // M, C, J, H // extract the word re =/\ B [a-z] * \ B/gi from the string; // \ B indicates the word boundary str = "one two three four"; console. log (str. match (re); // one, two, three, four

Case 4 RegExp object instance attributes

Var re =/[a-z]/I; console. log (re. source); // output the [a-z] string // note that the console is used directly. log (re) will output the regular expression along with the forward slash and sign. This is re. toString method defined var re =/[A-Z]/; // exec method after execution, modified the re lastIndex attribute, var str = "Hello, World !!! "; Var arr = re.exe c (str); console. log (re. lastIndex); // 0 because the global flag re =/[A-Z]/g; arr = re.exe c (str); console. log (re. lastIndex); // 1 arr = re.exe c (str); console. log (re. lastIndex); // 7 var re =/[A-Z]/; var str = "Hello, World !!! "; Re. lastIndex = 120; var arr = re.exe c (str); console. log (re. lastIndex); // 0

Case 5 Static attributes of RegExp objects

// Input the final string used for matching (the string passed to the test, exec method) var re =/[A-Z]/; var str = "Hello, World !!! "; Var arr = re.exe c (str); console. log (RegExp. input); // Hello, World !!! Re.exe c ("tempstr"); console. log (RegExp. input); // It is still Hello, World !!!, Because tempstr does not match // The character re =/[a-z]/g; str = "hi"; re. test (str); console. log (RegExp. lastMatch); // h re. test (str); console. log (RegExp ["$ &"]); // I, $ & is the short name of lastMatch, but it is required because it is not a valid variable name .. // The Last matched group re =/[a-z] (\ d +)/gi; str = "Class1 Class2 Class3"; re. test (str); console. log (RegExp. lastParen); // 1 re. test (str); console. log (RegExp ["$ +"]); // 2 // leftContext returns the character from the starting position of the string to the position before the last match. // rigthContext returns the string to start from the last match position. character re =/[A-Z]/g between the end of the string; str = "123ABC456"; re. test (str); console. log (RegExp. leftContext); // 123 console. log (RegExp. rightContext); // BC456 re. test (str); console. log (RegExp ["$ '"]); // 123A console. log (RegExp ["$ '"]); // C456

Case 6 precautions for using RegExp to construct a function

Var str = "\? "; Console. log (str); // only outputs? Var re = /\? // Will match? Console. log (re. test (str); // true re = new RegExp ("\? "); // Error, because \ is an escape character in the string \? Equivalent? To get \?, Yes \\? Re = new RegExp ("\\? "); // Correct, will match? Console. log (re. test (str )); // true use special characters in the regular expression // use hexadecimal numbers in ASCII mode to represent special characters var re =/^ \ x43 \ x4A $ /; // match CJ console. log (re. test ("CJ"); // true // You can also use the octal mode re =/^ \ 103 \ 112 $/; // match the CJ console. log (re. test ("CJ"); // true // you can also use Unicode encoding re =/^ \ u0043 \ u004A $/; // Unicode, which must start with u, then there is the four-digit hex representation of the character encoding console. log (re. test ("CJ "));

The above is a simple application of the five common functions, hoping to help you learn.

Related Articles:Common functions of js Regular Expressions

Articles you may be interested in:
  • View the performance of js Regular Expressions by using trim prototype functions
  • Javascript mobile phone number Regular Expression verification function
  • JavaScript Regular Expression verification function code
  • Use regular expressions to replace all js functions.
  • JavaScript Regular Expression-based numeric judgment Function
  • Use regular expressions to determine whether the string is a js Function Code of Chinese characters or pinyin
  • Explanation of the match function of js Regular Expressions
  • Test function for js Regular Expressions
  • Use of js trim () Functions and Regular Expressions

Related Article

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.