Paste (alias name): Javascript Regular Expression test webpage

Source: Internet
Author: User

Paste (alias name): Javascript Regular Expression test webpage
How do you test regular expressions? Write the Code directly in a large segment, wait for the program to run there, and then use alert to display the result? Or is it possible to temporarily write a page and discard it after testing?

 

You can try this page. Although it is rough, it is enough to test the regular expression :))

 

I. Description:

1. My intention is to exchange technology. I am very good at the level. Therefore, there will be deficiencies in the Code posted.

2. The code can be modified at will. If you think the code you modified is better than mine, you can post it. Thank you very much.

3. I am not very familiar with the Javascript specification. My experience comes from development. Therefore, if you have better opinions on it, thank you very much.

 

Ii. Test cases:

 

1. check whether there are non-numeric characters in the string:

Test RegEx:/d

Test RegEx flags: G

Test string: 1234g56t78

 

2. Check whether the strings contain non-0-9, ~ Z, a-Z, _ characters:

Test RegEx:/W

Test RegEx flags: G

Test string: I'm a pig! Yes I'm!

 

3. Match the real number in the string:

 

Test RegEx: ([/d] +])/. ([/d] +)

Test RegEx flags: G

Test string: float1234.58.723c65.183

 

3. program code:

 

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">

<HTML>

<Head>

<Title> JavaScript RegEx test page </title>

<Meta HTTP-EQUIV = "Content-Type" content = "text/html; charset = gb2312">

<SCRIPT>

/**

* Method: test the regular expression function.

* @ Param method: Method of the regular expression class received

*/

Function check (method)

{

// Convert the sign of the regular expression entered by the user to lowercase

Form1.flags. value = form1.flags. value. tolowercase ();

// If it contains characters other than G, I, and m, the system prompts and returns

If (form1.flags. value. Search (/[^ G | I | M]/g )! =-1)

{

Alert ("flags only can be G, I, m ");

Return;

}

// Create a regular expression using the string and flag entered by the user

VaR Re = new Regexp (form1.regex. Value, form1.flags. value );

// Obtain and display the string format of the generated Regular Expression

Destregex. innertext = Re. tostring () + '';

// Define the return value

VaR CR;

Ex = "New Regexp ('" + form1.regex. Value + "', '" + form1.flags. Value + "').";

// Call the method selected

Switch (method)

{

Case '0': // exec method of the Regular Expression

Cr = re.exe C (form1.string. value );

Ex = ex + "Exec ('" + form1.string. Value + "')";

Break;

Case '1': // The Test Method of the Regular Expression

Cr = Re. Test (form1.string. value );

Ex = ex + "test ('" + form1.string. Value + "')";

Break;

Case '2': // match method of the string class

Cr = form1.string. value. Match (re );

Ex = "'" + form1.string. Value + "'. Match (" + RE. tostring () + ")";

Break;

Case '3': // string-type Search Method

Cr = form1.string. value. Search (re );

Ex = "'" + form1.string. Value + "'. Search (" + RE. tostring () + ")";

Break;

Case '4': // string class replace Method

Cr = form1.string. value. Replace (re );

Ex = "'" + form1.string. Value + "'. Replace (" + RE. tostring () + ")";

Break;

Case '5': // string class split method

Cr = form1.string. value. Split (re );

Ex = "'" + form1.string. Value + "'. Split (" + RE. tostring () + ")";

Break;

}

// Obtain and display the expression

Expression. innertext = ex;

// Obtain and display the calculation result type

Returntype. innertext = typeof (CR );

 

// Define the result

VaR result = '';

If (CR! = NULL & typeof (CR) = 'object' & Cr. length! = NULL) // if the calculation result is an array, the values of all arrays are taken out.

{

For (I = 0; I <CR. length; I ++)

{

Result + = "array [" + I + "] = '" + Cr [I] + "'/N ";

}

}

Else

If (CR! = NULL) // if the calculation result is not null, the value of the calculation result is taken out.

{

Result = CR;

}

// Obtain and display the result

Matchresult. innertext = Result + '';

// Obtain and display the lastindex attribute of the Regular Expression

Lastindex. innertext = Re. lastindex + '';

}

</SCRIPT>

</Head>

 

<Body align = center>

<Form name = "form1" method = "Post" Action = "" align = center>

<Table border = "1">

<Tr>

<TD colspan = "2" align = "center"> test JavaScript RegEx </TD>

</Tr>

<Tr>

<TD> test RegEx: </TD>

<TD> <input name = "RegEx" type = "text"> </TD>

</Tr>

<Tr>

<TD> test RegEx flags: </TD>

<TD> <input name = "Flags" type = "text" id = "Flags"> </TD>

</Tr>

<Tr>

<TD> test string: </TD>

<TD> <input name = "string" type = "text"> </TD>

</Tr>

<Tr>

<TD> select method: </TD>

<TD align = "center">

<Select name = "select" onchange = "check (this. Value)">

<Option value = "0"> exec </option>

<Option value = "1"> test </option>

<Option value = "2"> match </option>

<Option value = "3"> Search </option>

<Option value = "4"> replace </option>

<Option value = "5"> split </option>

</SELECT>

<Input value = 'run' onclick = "check (form1.select. Value)" type = button>

</TD>

</Tr>

<Tr>

<TD> DEST RegEx: </TD>

<TD style = "color: Blue" id = destregex> & nbsp; </TD>

</Tr>

<Tr>

<TD> DEST expression: </TD>

<TD style = "color: Blue" id = expression> & nbsp; </TD>

<Tr>

<Tr>

<TD> return type: </TD>

<TD style = "color: darkred" id = returntype> & nbsp; </TD>

</Tr>

<Tr>

<TD> result: </TD>

<TD style = "color: Red" id = matchresult> & nbsp; </TD>

</Tr>

<Tr>

<TD> RegEx lastindex: </TD>

<TD style = "color: Red" id = lastindex> & nbsp; </TD>

</Tr>

</Table>

</Form>

</Body>

</Html>

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.