Mootools 1.2 Regular Expression

Source: Internet
Author: User
Tags mootools

If you are not familiar with how to use regular expressions (regular expression (regex), I strongly recommend that you take some time to look at some links in this article, especially the link to the "more learning" section at the end of the article. Today, we will only talk about the most basic usage of regular expressions. Regular Expressions can do much more than what we are talking about today.
Basic usage
Test () method
A regular expression can be a simple string that you want to match. Although JavaScript itself provides its own test () method for RegExp objects, the test () method of MooTools is easier to use and regular expressions are easier to use in JavaScript.
For beginners, let's take a look at the simplest usage of the test () method and look for a specific string in a large string:
Reference code:
Copy codeThe Code is as follows:
// We Need to find in this string
Var string_to_test = "Match anything in here ";
// The regular expression we are looking
Var regular_expression = "anything ";
// Apply a regular expression and return true or false
Var result = string_to_test.test (regular_expression );
// The result is now true.

This is basically similar to the action of the contains () function. However, the contains function searches for the complete word, while the regular expression matches any place where it appears. For example, in the following example, the contains () method does not return true, while the test () method returns true. (Fdream Note: According to the taoyu3161212 reminder, this statement is incorrect. In fact, the contains () method can specify two parameters. The first parameter is the string to be searched, and the second parameter is the separator string. Only when the second parameter is specified, contains () the method returns false, which is actually the contains () method of array .)
Reference code:
Copy codeThe Code is as follows:
Var string_to_match = "anything else ";
// Return true
String_to_match.contains ('nything ')
// Return false
String_to_match.contains ('nything ','')
// Return true
String_to_match.contains ('anything ')
// Return true
String_to_match.test ('nything ');

Note that, unless you specify it explicitly, regular expressions are case sensitive (case sensitive ), therefore, if you search for "Match" in a string containing "match", false is returned. You can try it in the following example:
Reference code:
Copy codeThe Code is as follows:
Var regex_demo = function (){
Var test_string = $ ('regex _ repeated value'). get ('value ');
Var regex_value = $ ('regex _ expect Match'). get ('value ');
Var test_result = test_string.test (regex_value );
If (test_result ){
$ ('Regex _ response result'). set ('html', "matched ");
}
Else {
$ ('Regex _ matched result'). set ('html', "didn't match ");
}
}

Note that regular expressions have some special characters and you need to use them with caution. If you input any of these characters into the following regular expression text box, an error will occur. In this case, you need to refresh the page to continue the following demo.
-. * +? ^ $ {} () | []/\
String to be tested:
Regular Expression
Case Insensitive
In many cases, you do not need to care about the case of the items you want to match. If you do not want a regular expression that is case sensitive, you can add the parameter "I" when calling the test () method ":
Reference code:
Copy codeThe Code is as follows:
// We Need to find in this string
Var string_to_test = "IgNorE CaSe ";
// Return false
String_to_test.test ("ignore ");
// Return true
String_to_test.test ("ignore", "I ");

Technically, you can pass multiple parameters to the test () method. However, JavaScript currently only supports three regular expression parameters (two of them are in test () is enabled by default). During this period, you may only use the "I" parameter ". You can continue to test the differences in case matching:
Reference code:
Copy codeThe Code is as follows:
Var regex_demo = function (){
// Obtain the string to be tested from the input text box
Var test_string = $ ('regex _ 2_value '). get ('value ');
// Obtain the regular expression from the input text box
Var regex_value = $ ('regex _ 2_match '). get ('value ');
// If we need to ignore the case sensitivity
Var regex_param = "";
If ($ ('regex _ 2_param '). checked ){
Regex_param = "I ";
}
// Run the test () method and obtain the result.
Var test_result = test_string.test (regex_value, regex_param );
// Update the display area
If (test_result ){
$ ('Regex _ 2_result '). set ('html', "matched ");
}
Else {
$ ('Regex _ 2_result '). set ('html', "didn't match ");
}
}

String to be tested:
Regular Expression
Case Insensitive
Interesting things
Now we have learned Simple Matching. We can start to look at some of the more impressive aspects of regular expressions. This won't cover anything that may be related to regular expressions-we will pick some more direct and useful features.
Use ^ to start String Matching
The "^" operator of the regular expression allows you to match at the beginning of a line of string, regardless of whether the character is matched or not. Place it at the beginning of the regular expression you want to match, as shown below:
Reference code:
Copy codeThe Code is as follows:
// String to be tested
Var string_to_test = "lets match at the beginning"
// Test whether the string starts with lets and returns true.
Var is_true = string_to_test.match ("^ lets ");
As expected, if the expression is not at the beginning of the string, the test returns false:
// String to be tested
Var string_to_test = "lets match at the beginning ";
// Test whether the string starts with match and returns false
Var is_false = string_to_test.match ("^ match ");

Continue to test the following:
String to be tested:
Regular Expression
Case Insensitive
Use $ to match the end of a string
The functions of the "$" operator are similar to those of the "^" operator, but there are two differences:
It matches the end of a string instead of the beginning.
It is placed at the end of the regular expression rather than the beginning.
In addition, all of its functions are the same as you would expect:
Reference code:
Copy codeThe Code is as follows:
// String to be tested
Var string_to_test = "lets match at the end ";
// Test whether the string ends with end. Return true.
Var is_true = string_to_test.match ("end $ ");
// Test whether the string ends with the character string. the return value is false.
Var is_false = string_to_test.match ("the $ ");

By using these two operators together, you can perform a very clean test: You can check whether a string contains only the content of the expression you want to match, without anything else.
Reference code:
Copy codeThe Code is as follows:
// String to be tested
Var string_to_test = "lets match everything ";
// Test whether the string is exactly the same as "lets match everything" and returns true.
Var is_true = string_to_test.match ("^ lets match everything $ ");
// Test whether the string is exactly the same as "lets everything" and returns false.
Var is_false = string_to_test.match ("^ lets everything $ ");

String to be tested:
Regular Expression
Case Insensitive
Character Set
Character Set is another regular expression tool that allows you to match multiple specific characters (A or Z) and A series of characters (A to Z ). For example, if you want to test whether a string contains the word moo or boo, you can put these two characters in square brackets [] of a regular expression:
Reference code:
Copy codeThe Code is as follows:
// Test the moo string
Var first_string_to_test = "cows go moo ";
// Test the boo string
Var second_string_to_test = "ghosts go boo ";
// This matches the first string but does not match the second string
Var returns_true = first_string_to_test.test ("moo ");
Var returns_false = second_string_to_test ("moo ");
// This matches the second string but does not match the first string
Returns_false = first_string_to_test.test ("boo ");
Returns_true = second_string_to_test.test ("boo ")
// This matches both the first and second strings.
Returns_true = first_string_to_test ("[mb] oo ");
Returns_true = second_string_to_test ("[mb] oo ");

String 1 to be tested:
String 2 to be tested:
Regular Expression
Case Insensitive
To match a series of characters, you can take out the first and last characters of the series separately and connect them with a connector. You can define a series of numbers or characters in this way:
Reference code:
Copy codeThe Code is as follows:
Var string_to_test = "B or 3 ";
// Returns true if a, B, c, or d is matched.
String_to_test.test ("[a-d]");
// Match 1, 2, 3, 4, or 5. Return true.
String_to_test.test ("[1-5]");

If you want to match multiple character sets, you can put your character set in a square brackets [] and separate it with the "|" operator.
Reference code:
Copy codeThe Code is as follows:
Var string_to_test = "B or 3 ";
// Returns true if a to d or 1 to 5 is matched.
String_to_test.test ([[a-d] | [1-5]);

String 1 to be tested:
String 2 to be tested:
Regular Expression
Case Insensitive
EscapeRegExp () method
When you see the regular expression method, you may find it very difficult to match some special characters. For example, what if you want to find "[stuff-in-here]" or "$300" in a string? You can manually add '\' before each special character you want to ignore.
Reference code:
Copy codeThe Code is as follows:
// The string to be matched. Note [,],-, and $
Var string_to_match = "[stuff-in-here] or $300 ";
// Incorrect Matching Method
String_to_match.test ("[stuff-in-here]");
String_to_match.test ("$300 ");
// Correct Matching Method
// Pay attention to [,],-, and \ above $ \
String_to_match.test ("\ [stuff \-in \-here \]");
String_to_match.test ("\\$ 300 ");

This is often a headache for processing regular expressions, especially when you are not fully familiar with them. For reference, special characters to be escaped in regular expressions include:
-. * +? ^ $ {} () | []/\
Fortunately, MooTools provides the escapeRegExp () function to ensure that your regular expression is correctly converted. This is another string function, so you only need to call this method on the regular expression string you want to match before you start searching.
Reference code:
Copy codeThe Code is as follows:
// The Escape string
Var unescaped_regex_string = "[stuff-in-here]";
// Escape this string
Var escaped_regex_string = unescaped_regex_string.escapeRegExp ();
// The Escape string is "\ [stuff \-in \-here \]"

Note: this means that any special characters used in the regular expression must be escaped before being added:
Reference code:
Copy codeThe Code is as follows:
// The string to be escaped
Var unescaped_regex_string = "[stuff-in-here]";
// Escape this string and match it from the beginning
Var escaped_regex_string = "^" + unescaped_regex_string.escapeRegExp ();
// The escaped_regex_string is now "^ \ [stuff \-in \-here \]"

Continue to test the difference between using escapeRegExp () and not using in the following example:
Reference code:
Copy codeThe Code is as follows:
Var regex_demo = function (){
// Obtain the string to be tested
Var test_string_1 = $ ('regex _ 7_value_1 '). get ('value ');
// Obtain the regular expression to be used
Var regex_value = $ ('regex _ 7_match '). get ('value ');
// Check if we want to escape the Regular Expression
If ($ ('regex _ 7_escape '). checked ){
// If yes, escape
Regex_value = regex_value.escapeRegExp ();
}
// Check whether case sensitivity is ignored.
Var regex_param = "";
If ($ ('regex _ 7_param '). checked ){
Regex_param = "I ";
}
// Run the test
Var test_result_1 = test_string_1.test (regex_value, regex_param );
If (test_result_1 ){
$ ('Regex _ 7_result_1 '). set ('html', "matched ");
}
Else {
$ ('Regex _ 7_result_1 '). set ('html', "didn't match ");
}
}

String 1 to be tested:
Regular Expression
Escape Regular Expressions
Case Insensitive
Remember, the demo may not run properly because you have used special characters without escaping. Do not be surprised when the example cannot run, because you are always playing with these things.
Learn more

Download a zip package containing everything you need

Regular-Expressions.info is a good place for reference and learning-a site worth spending some time browsing. For those who are familiar with Perl or are familiar with different languages, the section on regular expressions in Robert's Perl tutorial explains some basic concepts very well. Similarly, Stephen Ramsay has written a tutorial on Unix regular expressions, explaining some of these concepts in a very clear and straightforward way.

Another good thing is the regular expression library, which has countless Regular Expression examples to complete various common tasks. Finally, if you have the courage, you should take some time to look at Mozilla's reference manual for JavaScript regular expressions. This may be very many, but it is extremely useful. If you want to take a look at the regular expressions in MooTools, you can take a look at the document of the test () function.

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.