Mootools 1.2 Tutorial Regular expression _mootools

Source: Internet
Author: User
Tags first string regular expression mootools
If you're not familiar with how to use regular expressions (regular expression (regex)), I strongly recommend that you take a certain amount of time to take a good look at some of the links in this article, especially the "more Learning" section at the end of the article. Today we are just talking about the most basic use of regular expressions, and regular expressions can do far more than what we are talking about today.
Basic usage
Test () method
The simple thing is that a regular expression can be a simple string that you want to match. Although JavaScript itself has provided its own test () method for the RegExp object, the MooTools test () method is better used, and it is easier to use regular expressions in JavaScript.
For starters, let's look at the simplest use of the test () method to find a specific string in a large string:
Reference code:
Copy Code code as follows:

We're going to find in this string
var string_to_test = "Match anything in";
The regular expression we're looking for
var regular_expression = "Anything";
Apply a regular expression, return True or False
var result = String_to_test.test (regular_expression);
Result is now true

This is similar to the behavior of the contains () function, but the contains is a complete word lookup, and the regular expression matches wherever it appears. For example, in the following example, the contains () method will not return true, and the test () method will return true. (Fdream Note: By taoyu3781212 's reminder, this statement is not correct.) In fact, the contains () method can specify two arguments, the first argument is the string to look for, the second is a delimited string, and the contains () method returns false only if the second argument is specified, which is actually the contains () method of the array. )
Reference code:
Copy Code code as follows:

var string_to_match = "anything else";
Returns True
String_to_match.contains (' nything ')
return False
String_to_match.contains (' nything ', ')
Returns True
String_to_match.contains (' anything ')
Returns True
String_to_match.test (' nything ');

Also note that unless you explicitly specify that the regular expression is case-sensitive (case-sensitive), finding "match" in a string containing "match" returns false. You can try this in the following example:
Reference code:
Copy Code code as follows:

var Regex_demo = function () {
var test_string = $ (' regex_1_value '). Get (' value ');
var regex_value = $ (' Regex_1_match '). Get (' value ');
var test_result = test_string.test (Regex_value);
if (Test_result) {
$ (' Regex_1_result '). Set (' HTML ', ' matched ');
}
else {
$ (' Regex_1_result '). Set (' HTML ', "didn ' t match");
}
}

Note that there are some special characters in the regular expression that you need to use with care. If you enter any of these characters into the following regular expression text box, you will have an error, and this time you will need to refresh the page to continue with the following demo example.
- . * + ? ^ $ { } ( ) | [ ] / \
The string to test:
Regular expressions
Ignore case
In many cases, you don't need to care about the case of the item you want to match. If you don't want a regular expression to be case sensitive, you can add a parameter "I" When you call the test () method:
Reference code:
Copy Code code as follows:

We're going to find in this string
var string_to_test = "IgNorE case";
return False
String_to_test.test ("Ignore");
Returns True
String_to_test.test ("Ignore", "I");

Technically, you can pass multiple arguments to the test () method, but since JavaScript now supports only 3 regular expression parameters (of which 2 are enabled by default in the test () method), you may only use the parameter "I" in this period. You can continue to test the different case matching:
Reference code:
Copy Code code as follows:

var Regex_demo = function () {
Get the string you want to test from the input text box
var test_string = $ (' regex_2_value '). Get (' value ');
Get regular expressions from an input text box
var regex_value = $ (' Regex_2_match '). Get (' value ');
If we need to ignore the case
var regex_param = "";
if ($ (' Regex_2_param '). Checked) {
Regex_param = "I";
}
Run the test () method and get the result
var test_result = test_string.test (Regex_value, Regex_param);
Update Results display Area
if (Test_result) {
$ (' Regex_2_result '). Set (' HTML ', ' matched ');
}
else {
$ (' Regex_2_result '). Set (' HTML ', "didn ' t match");
}
}

The string to test:
Regular expressions
Ignore case
Interesting things.
Now that we've learned a simple match, we can start looking at some of the more impressive aspects of regular expressions. This does not cover everything that might be relevant to regular expressions--we will pick something more direct and useful.
Use ^ start match from string
The "^" operator of the regular expression allows you to match at the beginning of a line of strings, regardless of whether there is a match behind the character. Put it at the beginning of the regular expression you want to match, as follows:
Reference code:
Copy Code code as follows:

The string we want to test
var string_to_test = "Lets match at the beginning"
Test whether this string starts with lets and returns True
var is_true = String_to_test.match ("^lets");
As you would expect, if this expression is not at the beginning of the string, the test returns false:
The string we want to test
var string_to_test = "Lets match at the beginning";
Test if this string starts with match and returns false
var is_false = String_to_test.match ("^match");

Continue to test the following:
The string to test:
Regular expressions
Ignore case
Use the end of the $ match string
The function of the "$" operator is similar to that of "^", but there are two different points:
It matches the end of a string rather than the beginning.
It is placed at the end of the regular expression, not the beginning.
In addition, all of its features are the same as you would expect:
Reference code:
Copy Code code as follows:

The string we want to test
var string_to_test = "Lets match at the end";
Test whether this string ends with end and returns True
var is_true = String_to_test.match ("end$");
Test this string is not at the end, return false
var is_false = String_to_test.match ("the$");

By using these two operators together, you can do a very clean test: You can check that a string is not just the content of the expression you want to match without anything else.
Reference code:
Copy Code code as follows:

The string we want to test
var string_to_test = "Lets match everything";
Test this string is not exactly the same as "lets match everything", returns true
var is_true = String_to_test.match ("^lets match everything$");
Test this string is not exactly the same as "lets everything", return false
var is_false = String_to_test.match ("^lets everything$");

The string to test:
Regular expressions
Ignore case
Character
A character set is another regular expression tool that allows you to match multiple specific characters (A or z) and a series of characters (A through Z). For example, if you want to test whether a string contains a word moo or boo, you can use the character set to place the two characters in the square brackets [] of a regular expression:
Reference code:
Copy Code code as follows:

To test the string used for Moo
var first_string_to_test = "Cows go moo";
Test the string for Boo
var second_string_to_test = "Ghosts Go Boo";
This matches the first string and 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 and 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");

The string to test is one:
The string to test two:
Regular expressions
Ignore case
To match a series of characters, you can separate the first and last characters of this series of characters and then connect them with a connector (-). You can define a series of numbers or characters in this way:
Reference code:
Copy Code code as follows:

var string_to_test = "B or 3";
Match A, B, C, or D, return true
String_to_test.test ("[a-d]");
Match 1, 2, 3, 4, or 5. Returns True.
String_to_test.test ("[1-5]");

If you want to match in more than one character set, you can put your character set in a square bracket [], and then use the "|" Operators are separated.
Reference code:
Copy Code code as follows:

var string_to_test = "B or 3";
Match A to D or 1 to 5, return True
String_to_test.test ([[a-d] | [1-5]]);

The string to test is one:
The string to test two:
Regular expressions
Ignore case
Escaperegexp () method
When you see a method established by a regular expression, you may find it difficult to match some special characters. For example, what if you want to find "[Stuff-in-here]" or "$300" in a string? You can do this by manually adding ' \ ' in front of each special character you want to ignore.
Reference code:
Copy Code code as follows:

We want to match the strings, note [,],-and $
var string_to_match = "[Stuff-in-here] or $300";
Incorrect way of matching
String_to_match.test ("[Stuff-in-here]");
String_to_match.test ("$300");
The right way to match
Note [,],-and $ before the \
String_to_match.test ("\[stuff\-in\-here\]");
String_to_match.test ("\$300");

This is often the place to deal with regular expression headaches, especially when you're not completely familiar with them. As a reference, special characters that need to be escaped in a regular expression include:
- . * + ? ^ $ { } ( ) | [ ] / \
Fortunately, MooTools provides the Escaperegexp () function to ensure that your regular expression is correctly escaped. This is another string function, so you just need to call this method on the regular expression string that you want to match before you start looking.
Reference code:
Copy Code code as follows:

The string we want to escape
var unescaped_regex_string = "[Stuff-in-here]";
Escape this string
var escaped_regex_string = Unescaped_regex_string.escaperegexp ();
The escaped string is "\[stuff\-in\-here\]"

Note that this means that any special characters you want to use in regular expressions must be added after escaping:
Reference code:
Copy Code code as follows:

String that needs to be escaped
var unescaped_regex_string = "[Stuff-in-here]";
Escape this string, match from the beginning
var escaped_regex_string = "^" + unescaped_regex_string.escaperegexp ();
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 Code code as follows:

var Regex_demo = function () {
Get the string to test
var test_string_1 = $ (' regex_7_value_1 '). Get (' value ');
Get the regular expression to use
var regex_value = $ (' Regex_7_match '). Get (' value ');
Check if we're going to escape the regular expression.
if ($ (' Regex_7_escape '). Checked) {
If yes, we are escaping.
Regex_value = Regex_value.escaperegexp ();
}
Check to see if we're ignoring the case.
var regex_param = "";
if ($ (' Regex_7_param '). Checked) {
Regex_param = "I";
}
Run Tests
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");
}
}

The string to test is one:
Regular expressions
To escape from a regular
Ignore case
Remember, you may not be able to run the demo example because you have used special characters that are not escaped, so don't be surprised when the example doesn't work because you've been playing with these things all the time.

Learn More

Download a Zip package that contains everything you need to start

Regular-expressions.info is a great place to reference and learn-a site that is worth spending some time browsing. For those who are familiar with Perl or who are familiar with different language differences, the section on regular expressions inRobert's Perl tutorial is a great way to explain some basic concepts. Similarly, Stephen Ramsay has written a tutorial on UNIX regular Expressions , explaining some of these concepts in a very clear and straightforward manner.

Another nice place is the regular expression library , which has countless regular expression examples to complete a wide variety of common tasks. Finally, if you have the guts, you should take some time to look at Mozilla's javascript regular expression reference manual . This may be a lot, but it is extremely useful. If you want to take a look at the MooTools side of the story, look at the document for the test () function .

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.