Regular Expression of niuyun javascript (II), javascript Regular Expression

Source: Internet
Author: User

Regular Expression of niuyun javascript (II), javascript Regular Expression

Next to the basic javascript Knowledge mentioned in the previous article, let's talk about the content of javascript Regular Expressions today.

At this point, we need to first ask what is a regular expression and what can it do?

Regular Expression: a regular expression is a text mode consisting of common characters (such as a to z) and special characters (called metacharacters. This mode describes one or more strings to be matched when searching the text subject.
A regular expression is used as a template to match a character pattern with the searched string.

Function: test the mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called the replacement text for Data Validity verification. You can use a regular expression in the document to identify a specific text, and then you can delete it all or replace it with another text. extract a substring from the string according to the pattern matching. It can be used to search for specific text in text or input fields.

Well, the definition is always abstract. To learn more about regular expressions, I recommend such a Website: Regular Expressions in javascript. Here is a small example for your reference.

? It indicates that only one character can be entered or no character (0, 1 );

<script language="javascript" type="text/javascript">var reg = /ab?c/g;var str = "abc abbc adbc ac";var found = str.match(reg);alert(found);</script>
You can guess the input result, such:

+ Indicates that more than one character is input, including one character. (1 ,.....);

<script language="javascript" type="text/javascript">var reg = /ab+c/g;var str = "abc abbc adbc ac";var found = str.match(reg);alert(found);</script>
Input result:

* Indicates Entering zero or more characters, including zero. (0 ,.....)

<script language="javascript" type="text/javascript">var reg = /ab*c/g;var str = "abc abbc adbc ac";var found = str.match(reg);alert(found);</script>
Output result:

Once again, regular expressions have a lot of content, so we need to practice well and make a summary. These are some practical examples and are very important. For example, we need to use regular expressions to limit the number of input digits and find and replace the phone number in the office.

Every summary is a kind of sublimation of knowledge. In thinking, we have consolidated our knowledge, summarized our daily work well, and fixed our knowledge.

If you have any comments or suggestions, you will be eager to make a decision.



Javascript Regular Expression Verification

It turns out that your decimal point can end with 0...

/^ 0 $ | ^ [1-9] + \ d * (\. \ d * [1-9])? $ | ^ 0 \. \ d + $/

It seems that you are considerate and considerate.

There is also a simple method of test, without the need to match
If (/^ 0 $ | ^ [1-9] + \ d * (\. \ d * [1-9])? $ | ^ 0 \. \ d + $/. test (val )){
Alert ("OK ");
} Else {
Alert ("NO ");
}

Javascript Regular Expression

The preceding <(. *)> can match the abc in a text such as <abc>.

The following \ 1 is a reference to it. With \/added, you can get it. </abc>

After the combination, you can match the text such as <abc> 123456789abc </abc>.

Of course, since it is a group, you can also give it a name to facilitate the call.

Compare the following:

<(? <Aaa>. *)>. * <\/\ k <aaa>

The group name here is aaa

-----------------------------------------------

\ 1 is to apply the content in the first bracket? Then \ 2 references the content in the second bracket.

This is indeed the case, but note that it is parentheses.

Let's take another example.

The original text is <abc c = ss> 00000000000 </abc c = ss>

You can use such a regular expression. Of course, this is just to describe the actual operation, but it is troublesome.

<(. *) \ S (c = ss)>. * <\/\ 1 \ s \ 2>

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.