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> </TD>
</TR>
<TR>
<TD> dest expression: </TD>
<Td style = "color: blue" ID = expression> </TD>
<TR>
<TR>
<TD> return type: </TD>
<Td style = "color: darkred" ID = returnType> </TD>
</TR>
<TR>
<TD> result: </TD>
<Td style = "color: red" ID = matchResult> </TD>
</TR>
<TR>
<TD> regex lastIndex: </TD>
<Td style = "color: red" ID = lastIndex> </TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>