JavaScript Regular Expression Usage Summary _ regular expression

Source: Internet
Author: User
JavaScript Regular expression usage:
Copy Code code as follows:

function Checkreg (myreg,mytext)
{
if (Myreg.test (MyText)
{
Alert ("OK");
return true;
}
Else
{
return false;
}
}

Another way of writing
Copy Code code as follows:

function Checkreg (myreg,mytext)
{
var pattern = Myreg; or var pattern = new RegExp (Myreg, "GI");
if (Pattern.test (MyText))
{
Alert ("OK");
}
}

Using the explicit constructor of REGEXP, the syntax is: New REGEXP ("pattern" [, "Flags"]).
Use the implicit constructor of RegExp, in plain text format:/pattern/[flags].
The pattern section is required for the regular expression schema text to be used. In the first way, the pattern part is in the form of a JavaScript string that needs to be enclosed in double quotes or single quotes; in the second way, the pattern portion is nested between two "/" and cannot be quoted.
The flags section sets the flag information for the regular expression and is optional. If the flags section is set, in the first way, in the form of a string, in the second way, the text is immediately after the last "/" character. Flags can be a combination of the following flag characters.
G is a global flag. If this flag is set, when a search and replace operation is performed on a text, all matching parts of the text will be in effect. If you do not set this flag, only the first matching content is searched and replaced.
I is ignoring the case flag. If this flag is set, the case is ignored when matching comparisons are made.
M is a multiline flag. If this flag is not set, then the meta character "^" matches only the starting position of the entire searched string, and the metacharacters markerrdquo only matches the end position of the searched string. 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 line), the markerrdquo can also match the position after "\ n" or "\ r" in the searched string (that is, the line end of the next line).
Because "\" in a JavaScript string is an escape character, when you use an explicit constructor to create a RegExp instance object, you should replace "\" in the original regular expression with "\".
Onkeyup= "Value=value.replace (/[^0-9\.) /g, '); '//Regular expressions can also be used to replace strings.

Below are some of the more specific additions to the cloud-dwelling community:
Copy Code code 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 to use the replacement code to achieve, we must learn this, with more. More articles can refer to the regular expression columns of the cloud-dwelling community.
Related Article

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.