JS regular implementation matches and processes specific information from a complex HTML code string

Source: Internet
Author: User

JS regular implementation matches and processes specific information from a complex HTML code string

Problem:

Now to find a particular piece of information from a complex HTML code string (including various HTML tags, numbers, Chinese, and so on) (a pair of "|" wrapped), and he was added to the coarse, plus the line processing.

Solution Ideas:

1. Match "|" with regular The number of occurrences, processing occurs exactly 2 times (the HTML string generally does not contain this character)

2. Use regular grouping to get "|" Between the contents and replace (add style)

Code:

functionspecialdeal () {htmlstr= Htmlstr.replace (/&nbsp;/, "); varREG0 =NewREGEXP ("\\|", "G"); varArray =Htmlstr.match (REG0); //when and only if the "|" appears The number of times equals 2 o'clock, the "|" The content is bold and underlined if(Array! =NULL&& array.size () = = 2) {     varreg1=NewREGEXP ("([^\\|] *)(\\|) ([^\\|] *)(\\|) ([^\\|] *) "," IG "); varresult =reg1.exec (HTMLSTR); if(Result) {Htmlstr= Htmlstr.replace (reg1,result[1] + "<u><b>" + result[3] + "</b></u>" + result[5]); }      }      returnhtmlstr;}

The difference and meaning of/i,/g,/ig,/gi,/m in regular expressions

/I (ignoring case)
/g (full-text lookup for all occurrences of matching characters)
/m (Multi-line lookup)
/gi (Full-text lookup, ignore case)
/ig (Full-text lookup, ignore case)

A big carding of knowledge points

JS Regular expression Writing, JS is commonly used in several ways: match, exec, replace

1, JS Regular expression

When writing a regular, if you use the RegExp object, the JS regular expression is written in the string, in particular need to pay attention to escape.

JS Code
var reg0=New RegExp ("(<span[^>]* (Courier New) [^>]*>) (\\w*)", "IG");  

Equivalent to:

var reg0=/(<span[^>]+courier new[^>]+>) (\w*)/ig;  
Modifier modifier description
I Performs a match that is not case sensitive.
G

Performs a global match (finds all matches rather than stops after the first match is found).

M Performs multi-line matching.
Square brackets

Square brackets are used to find characters in a range:

Expression description
[ABC] Look for any characters between square brackets.
[^ABC] Look for any characters that are not in square brackets.
[0-9] Look for any number from 0 to 9.
[A-z] Finds any characters from a to lowercase z from a small write.
[A-z] Look for any characters from uppercase A to uppercase Z.
[A-z] Look for any characters from uppercase A to lowercase z.
[ADGK] Finds any character within a given collection.
[^ADGK] Finds any character outside the given collection.
(Red|blue|green) Finds any of the specified options.
Metacharacters

Metacharacters (metacharacter) is a character that has a special meaning:

Metacharacters description The
. finds a single character, in addition to line breaks and line terminators.
\w to find word characters.
\w to find non-word characters.
\d to find numbers.
\d to find non-numeric characters.
\s to find white space characters.
\s to find non-whitespace characters.
\b matches the word boundary.
\b matches non-word boundaries.
\ find NUL characters.
\ n find line breaks.
\f Find a page break.
\ r find a carriage return.
\ t find tabs.
\v to find vertical tabs.
\xxx to find characters that are specified in octal number XXX.
\xdd to find the characters specified in hexadecimal digits dd.
\uxxxx finds Unicode characters that are specified in hexadecimal number xxxx.
Description of quantifier quantifiers
n+ Matches any string that contains at least one n.
N Matches any string that contains 0 or more N.
N? Matches any string that contains 0 or one n.
N{X} Matches a string that contains a sequence of X N.
N{x,y} Matches a string containing a sequence of X or Y N.
N{x,} Matches a string that contains at least X N of a sequence.
n$ Matches any string that ends with N.
^n Matches any string that begins with N.
? =n Matches any string immediately followed by the specified string n.
?! N Matches any string that does not follow the specified string n immediately thereafter.
RegExp Object Properties

Ff:firefox, Ie:internet Explorer

Property Description Ffie
Global RegExp whether the object has a flag g. 1 4
IgnoreCase RegExp whether the object has a flag I. 1 4
LastIndex An integer that indicates the character position at which to start the next match. 1 4
Multiline RegExp whether the object has a flag m. 1 4
Source The source text of the regular expression. 1 4

2. Match () method

Definition and usage

The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions.

The method is similar to IndexOf () and lastIndexOf (), but it returns the specified value instead of the position of the string.

Grammar
Stringobject.match (Searchvalue) stringobject.match (regexp)
Parameter description
Searchvalue Necessary. Specifies the string value to retrieve.
Regexp Necessary. A RegExp object that specifies the pattern to match. If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor and convert it to a RegExp object.
return value

The array that holds the matching results. The contents of the array depend on whether the regexp has global flag G.

Description

The match () method retrieves the string stringobject to find one or more text that matches the regexp. The behavior of this method depends to a large extent on whether RegExp has a flag g.

If RegExp does not have a flag G, then the match () method can only perform a match in Stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array that holds information about the matching text it finds. The No. 0 element of the array holds the matching text, while the rest of the elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array contains two object properties. The Index property declares the position of the starting character of the matching text in Stringobject, and the input property declares a reference to Stringobject.

If RegExp has the flag G, the match () method performs a global retrieval and finds all matching substrings in the stringobject. If no matching substring is found, NULL is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are very different from the former, and its array elements hold all the matching substrings in the stringobject, and there is no index attribute or input property.

Note: In global retrieval mode, match () does not provide information about the text that matches the subexpression, nor does it declare the location of each matched substring. If you need these globally retrieved information, you can use Regexp.exec ().

3. Exec () method

Definition and usage

The exec () method is used to retrieve the matching of regular expressions in a string.

Grammar
Regexpobject.exec (string)
Parameter description
String Necessary. The string to retrieve.
return value

Returns an array that holds the results of the match. If no match is found, the return value is null.

Description

The exec () method is powerful, is a common method, and is more complex to use than the test () method and the method that supports the String object of a regular expression.

If EXEC () finds the matched text, it returns an array of results. Otherwise, NULL is returned. The No. 0 element of this array is the text that matches the regular expression, and the 1th element is the text that matches the 1th sub-expression of regexpobject (if any), and the 2nd element is the text (if any) that matches the 2nd sub-expression of regexpobject, in such Push. In addition to the array element and the length property, the Exec () method returns two properties. The Index property declares the position of the first character of the matched text. The input property holds the string that was retrieved. We can see that when calling the Exec () method of a non-global REGEXP object, the returned array is the same as the array returned by the calling Method String.match ().

However, when Regexpobject is a global regular expression, the behavior of exec () is slightly more complex. It will begin retrieving string strings at the character specified by the LastIndex property of Regexpobject. When exec () finds text that matches the expression, it sets the Regexpobject LastIndex property to the next position of the last character of the matched text after the match. This means that you can iterate through all the matching text in a string by calling the Exec () method repeatedly. When exec () can no longer find a matching text, it returns null and resets the LastIndex property to 0.

Hints and Notes

Important: If you want to start retrieving a new string after you have completed a pattern match in a string, you must manually reset the LastIndex property to 0.

Tip: Note that exec () adds complete details to the array it returns, regardless of whether the regexpobject is a global mode. This is where EXEC () differs from String.match (), which returns much less information in global mode. So we can say that calling the Exec () method repeatedly in a loop is the only way to get complete pattern matching information for the global schema.

4. Replace () method

Definition and usage

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

Grammar
Stringobject.replace (regexp/substr,replacement)
Parameter description
Regexp/substr

Necessary. A RegExp object that specifies the substring or pattern to replace.

Note that if the value is a string, it is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first.

Replacement Necessary. A string value. A function that specifies replacement text or generates alternate text.
return value

A new string that is obtained after the first match or all matches of regexp have been replaced with replacement .

Description

The replace () method of the string Stringobject performs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement . If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matched substring.

replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the resulting string from a pattern match will be used for substitution.

Character substitution text
$, $ 、...、 The text that matches the 1th to 99th sub-expression in RegExp.
$& A substring that matches the regexp.
$` The text that is located to the left of the matching substring.
$ The text on the right side of the matching substring.
$$ Direct volume symbol.

Note: ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function instead of a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

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.