Javascript Regular Expression usage Summary

Source: Internet
Author: User

Javascript Regular Expression usage: Copy codeThe Code is as follows: function checkreg (myreg, mytext)
{
If (myreg. test (mytext)
{
Alert ("OK ");
Return true;
}
Else
{
Return false;
}
}

// ANOTHER METHODCopy codeThe Code is as follows: function checkreg (myreg, mytext)
{
Var pattern = myreg; // or var pattern = new RegExp (myreg, "gi ");
If (pattern. test (mytext ))
{
Alert ("OK ");
}
}

Use RegExp's explicit constructor. Syntax: new RegExp ("pattern" [, "flags"]).
Use RegExp's implicit constructor in plain text format:/pattern/[flags].
The pattern part is required for the regular expression pattern text to be used. In the first method, the pattern part exists as a JavaScript string and must be enclosed by double quotation marks or single quotation marks. In the second method, the pattern part is nested between two, quotation marks are not allowed.
The flags part sets the Flag Information of the regular expression, which is optional. If the flags part is set, it exists as a string in the first method; in the second method, it follows the last "/" character in the text form. Flags can be a combination of the following flag characters.
G is the global flag. If this flag is set, the search and replacement operations on a text will take effect for all matching parts of the text. If this flag is not set, only the first matched content is searched and replaced.
I is a case-insensitive flag. If this flag is set, Case sensitivity is ignored during Matching and comparison.
M is a multiline flag. If this flag is not set, the metacharacter "^" only matches the start position of the searched string, and the metacharacter "markerrdquo; only matches the end position of the string to be searched. If this flag is set, "^" can also match the position after "\ n" or "\ r" in the searched string (that is, the beginning of the next row, "markerrdquo;" can also match the position after "\ n" or "\ r" in the searched string (that is, the end of the next row.
Because "\" in the JavaScript string is an escape character, when you use an explicit constructor to create a RegExp instance object, replace "\" in the original regular expression.
Onkeyup = "value = value. replace (/[^ 0-9 \.]/g,''); "// regular expressions can also be used to replace strings.

Below are some of the notes added by the script home Editor:Copy codeThe Code is as follows: function cleanAndPaste (html ){
Html = html. replace (/<\/? SPAN [^>] *>/gi ,"");
Html = html. replace (/<(\ w [^>] *) class = ([^ |>] *) ([^>] *)/gi, "<$1 $3 ");
Html = html. replace (/<(\ w [^>] *) style = "([^"] *) "([^>] *)/gi, "<$1 $3 ");
Html = html. replace (/<(\ w [^>] *) lang = ([^ |>] *) ([^>] *)/gi, "<$1 $3 ");
Html = html. replace (/<\\? \? Xml [^>] *>/gi ,"");
Html = html. replace (/<\/? \ W +: [^>] *>/gi ,"");
Html = html. replace (//,"");

InsertHTML (html );
}

The above is a replacement code implemented using regular expressions. You must learn this and use more. For more articles, refer to the regular expression section of the script house.

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.