JavaScript Regular Expression Test page _ regular expression

Source: Internet
Author: User
Tags regular expression
How do you normally test regular expressions? Write directly in a large section of code, then wait for the program to run there, and then display the results with alert? Or do you write a page temporarily and throw it away after the test is done?



You can try my this page, although it is very rough, but the test regular expression is enough:)



One, Description:

1. My intention is communication technology. I have a very good level of food. As a result, there are deficiencies in the code that is posted.

2. Code can be arbitrarily modified. If you feel that you have modified the code better than mine, can post it, I am very grateful.

3. I'm not very familiar with JavaScript specifications. My experience comes from the development. Therefore, if you have a better view of it, I would like to thank you for your offer.



Second, test cases:



1. Check to see if there are any non-numeric characters in the string:

Test regex: \d

Test Regex Flags:g

Test string:1234g56t78



2. Check if there are characters in the string that are not 0-9, A~z, A-Z, _:

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



Third, the 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 to test the regular expression function

* @param method of receiving regular expression classes

*/

function Check (method)

{

Converts the flags of the regular expressions entered by the user to lowercase

Form1.flags.value = Form1.flags.value.toLowerCase ();



If it contains characters that are not G, I, M, Prompt and return

if (Form1.flags.value.search (/[^g|i|m]/g)!=-1)

{

Alert ("Flags only can be G, I, M");

Return

}



Establish regular expressions with user-entered strings and flags

var re = new RegExp (Form1.regex.value, Form1.flags.value);



Gets and displays the string form of the generated regular expression

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



Define return values

var Cr;



ex = "New RegExp (' + Form1.regex.value +" ', ' "+ Form1.flags.value +"). "



According to the method that the user chooses, make the corresponding call

Switch (method)

{

Case ' 0 '://Exec method of regular expression

CR = Re.exec (Form1.string.value);

ex = ex + "exec (' + form1.string.value + ')";

Break



Case ' 1 '://Regular Expression test method

CR = Re.test (Form1.string.value);

ex = ex + "Test (' + Form1.string.value + ')";

Break



Case ' 2 '://Match method for string class

CR = Form1.string.value.match (re);

ex = "' + Form1.string.value +" '. Match ("+ re.tostring () +");

Break



Case ' 3 '://Search method for String class

CR = Form1.string.value.search (re);

ex = "' + Form1.string.value +" '. Search ("+ re.tostring () +");

Break



Case ' 4 '://Replace method for string class

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

}



Get and display an expression

Expression.innertext = ex;



To obtain and display the type of the calculation result

Returntype.innertext = typeof (CR);



Define Results

var result = ';



if (CR!= null && typeof (CR) = = ' object ' && cr.length!= null)//If the result of the calculation is an array, remove the values of all the arrays

{

for (i = 0; i < cr.length; i++)

{

result = + "array[" + i + "] = '" + cr[i] + "' \ n";

}

}

Else

if (CR!= null)//If the result is not NULL, the value of the calculated result is fetched

{

result = CR;

}



Get and display the results

Matchresult.innertext = result + ';



Gets and displays the Lastindex property 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>

&LT;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>

&LT;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>

&LT;TD style= "Color:blue" id=destregex> </TD>

</TR>

<TR>

<td>dest expression:</td>

&LT;TD style= "Color:blue" id=expression> </TD>

<TR>

<TR>

<td>return type:</td>

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

</TR>

<TR>

<TD>result:</TD>

&LT;TD style= "color:red" id=matchresult> </TD>

</TR>

<TR>

<td>regex lastindex:</td>

&LT;TD style= "color:red" id=lastindex> </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.