JavaScript Regular Expressions

Source: Internet
Author: User

The use of regular expressions in program applications is very extensive.

First the regular expression is generally written between two slashes:/Regular Expression/

followed by two symbols ^ and $, the symbol ^ represents the beginning of a string, while $ represents the end of a string.

As an example:

1 <script>2         var str= ' asdfghjkl '; 3         var select=/^asd/; 4 </script>

This declares a string str, declaring a Select that represents all strings that begin with ASD.

1 <script>2     var str= ' asdfghjkl '; 3     var select=/jkl$/; 4 </script>

Represents all strings that end with JKL.

But how do you apply this regular expression? The test () method will be used.

The test () method is used to detect whether a string matches a pattern

Next to the code, the format is:

1 select.test (str)

This code has a return value, and if STR does conform to the regular expression requirement, it returns true, otherwise false, so you can use the conditional statement if.

For example:

 1  <script >2  var  str= ' asdfghjkl ' ;  3  var  select=/jkl$/ 4  if   ( Select.test (str)) { 5   alert (str);  6 }else  {  7  alert (' string does not meet the requirements! '  8  }  9  </script>  

Meet the requirements to eject the string, otherwise the popup ' string does not meet the requirements! '.

The regular expression has a lot of syntax, in addition to the above, there are:

/^abc$/: A string beginning with ABC and ending with ABC.

/abc/: A string containing the ABC anywhere.

/ab*/: A string has a followed by 0 or a number of B ...

/ab+/: A string has an a followed by one or more B.

/ab? /: A string has an a followed by 0 or a B.

/a?b+$/: 0 at the end of a string or a followed by one or several B ...

/ab{2,}/: A string has one a followed by at least two B.

/ab{2,3}/: A string has one a followed by at least two to three B.

Curly braces represent ranges, can have no maximum range, but must have a minimum range

+,$,* can be represented by the {range}.

/abc|abd/: There is either ABC or ABD in a string.

/(ABC|ABD) ef/: A string containing ABCEF or abdef.

/(A|B) *c/: Indicates that a string is mixed with a, b followed by a C.

/a.[0-9]/: Indicates that a string has an a followed by any character and a number.

/^. {3}$/: Represents a string of any three characters. (length is three characters)

/[ab]/: Indicates that a string has a or B.

/[0-9]%/: Represents a number that has one digit before a percent semicolon.

/[a-g]/: Indicates that a string contains one of lowercase letters A through G.

/^[a-za-z]/: Represents a string that begins with a single letter.

/,[a-za-z0-9]$/: Indicates that a string ends with a comma followed by a letter or a number.

JavaScript Regular Expressions

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.