How to use regular expressions in JavaScript and Java

Source: Internet
Author: User

Regular Expressions only have two functions: Pattern validation and replacement.

Before getting started, let's briefly introduce the basics of regular expressions. The following is an excerpt from my work:

Symbol used to match an element

The '.' symbol matches all characters, including spaces, TAB characters, and even line breaks.

'[]' Specifies the matching characters in square brackets. eg. the regular expression "t [aeio] n" only matches "Tan", "Ten", "Tin", and "ton ".

'(|)' | "Indicates the" or "operation. eg. to match "Toon", use "t (A | E | I | o | OO) N ". [] Cannot be used here, because [] can only match a single character.

'^' Indicates a negative sign. For example, [^ x] [A-Z] + indicates that the first character cannot be X

'\ S': the Space mark eg. "June 12,199 1", the expression is: [A-Z] + \ s + [0-9] {}, \ s * [0-9] {4 }, if you want to extract the month, you need to add parentheses for grouping ([A-Z] +) \ s + [0-9] {1, 2 }, \ s * [0-9] {4}, which involves replacing strings.

Symbol indicating the number of matches

 '*': O times or times '+': 1 time or multiple times '? ': 0 times or 1 time' {n} ': Match n times' {n, m} ': Match N-m times

For example, if 999-88789-9800, the regular expression is [0-9] {3} \-[0-9] {5} \-[0-9] {4 }, if 999887899800 is correct, you can change it to [0-9] {3 }\-? [0-9] {5 }\-? [0-9] {4}

Some quick symbols

\ D: [0-9] \ D: [^ 0-9] \ W: [A-Z0-9] \ W [^ A-Z0-9] \ s: [\ t \ n \ r \ f] \ s: [^ \ t \ n \ r \ F]

Therefore, we can change [0-9] {3} \-[0-9] {5} \-[0-9] {4}: \ D {3} \-\ D {5} \-\ D {4}

 

Below we will use some simple examples to introduce, it should be noted that the use of regular expressions in Java and JavaScript is a little different,Here I will only introduce the method used, not specific usage (For details, refer to javascript:Http://www.iteye.com/topic/481228,In Java:Http://blog.csdn.net/b10090120/article/details/7784410)

For strings:<Peter> <Lionel> \ "www.163.com \" <Kenny> <> 1234 5678Operation

In Javascript

<HTML>

<Head>

<Title> Reg </title>

<SCRIPT type = "text/JavaScript">

VaR STR = "<Peter> dsadasdas <Lionel> \" www.163.com \ "<Kenny> <> ";

VaR Reg =/(<[^>] *>)/g; // G (full-text search) I (case-insensitive) m (multi-row search)

// Query all matching substrings

VaR cc = Str. Match (REG );

Alert (CC [2]);

// Use Reg as a standard split Array

Arr = Str. Split (REG );

Alert (ARR [1]);

// Replace: Replace the substring that matches the regular expression, where the grouping string is placed between $1 and ~ $9 medium

Alert (Str. Replace (Reg, 'replace it '));

Alert (Str. Replace (Reg, " </img> "));

VaR REE =/(\ D) \ s (\ D )/;

Alert (Str. Replace (REE, "$2, $1"); // in this example, $1 indicates that the first group is 1234, and $2 indicates 5678.

// Indexof is commonly used in string search. The corresponding method for regular search is search.

Alert (Str. Search (REG); // returns the searched string starting with subscript 0 </SCRIPT>

</Head>

<Body>

Reg Testing

</Body>

</Html>

 

Note:

VaR Re =/@ (\ s +) + (\ s )? /G; // filter @ XX and replace it

STR = Str. replace (Re, '<a class = "marked-name" href = "<person>" rel = "weiboatem-username" name = "$1" >@$ 1 </a> ');

// Spliced Regular Expression

VaR userfacescn = "Smile ";

VaR userfaces = "pic_1.gif ";

VaR Reg = new Regexp ("\ [" + userfacescn + "\]", 'G ');

Weibocontent = weibocontent. Replace (Reg, "[" + userfaces + "]");

VaR osversion = "ubuntu 8"; 8 indicates the system master version.
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 submatch
Arr implements re.exe C (osversion );
Alert (ARR [0]); Complete matching of the entire osversion, that is, the Regular Expression
Alert (ARR [1]); 8. The first child matches, and the main version number can also be retrieved in this way.
Alert (ARR. Length); 2
Osversion = "ubuntu 8.10"; obtain the main version and minor version
Re =/^ [A-Z] + \ s + (\ D + )\. (\ D +)/I ;. is one of the metacharacters of a regular expression. to use its literal meaning, it must be escaped.
Arr = re.exe C (osversion );
Alert (ARR [0]); Complete osversion
Alert (ARR [1]); 8
Alert (ARR [2]); 10

 

 

Java

String S = "<Peter> dddd <Lionel> \" www.163.com \ "<Kenny> <> ";

Pattern P =
Pattern.Compile("(<[^>] *> )");

Matcherm = P. matcher (s );

List <String> Result =NewArraylist <string> ();

While(M. Find ()){

Result. Add (M. Group ());

}

For(String S1: result ){

System.Out. Print (S1 + ',');

}

Output result:<Peter>, <Lionel>, <Kenny>,

 

 

 

 

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.