前端之家收集整理的这篇文章主要介绍了
学习笔记——简单的正则表达式测试器,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<Meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Regular Expression Tester</title>
<style type="text/css">
body,td,th
{
font-family:Arial,Helvetica,sans-serif;
}
</style>
<script type="text/javascript">
function getRegExpFlags()
{
var regExpFlags='';
if(document.form1.chkGlobal.checked)
regExpFlags='g';
if(document.form1.chkCaseInsensitive.checked)
regExpFlags +='i';
if(document.form1.chkMultiLine.checked)
regExpFlags +='m';
return regExpFlags;
}
function doTest()
{
var testRegExp=new RegExp(document.form1.txtRegularExpression.value,getRegExpFlags());
if(testRegExp.test(document.form1.txtTestString.value))
document.form1.txtTestResult.value="Match Found! ";
else
document.form1.txtTestResult.value="Match Not Found";
}
function findMatches()
{
var testRegExp=new RegExp(document.form1.txtRegularExpression.value,getRegExpFlags());
var myTestString=new String(document.form1.txtTestString.value);
var matchArray=myTestString.match(testRegExp);
document.form1.txtTestResult.value=matchArray.join('\n');
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
Regular Expression:<br />
<label>
<input name="txtRegularExpression" type="text" id="txtRegularExpression" size="100" value=""/>
<br />
Global
<input name="chkGlobal" type="checkBox" id="chkGlobal" value="true" />
</label>
Case Insensitive
<label>
<input name="chkCaseInsensitive" type="checkBox" id="chkCaseInsensitive" value="true"/>
</label>
Multi Line
<label>
<input name="chkMultiLine" type="checkBox" id="chkMultiLine" value="true"/>
</label>
</p>
<p>
<label>
Test Text:<br />
<textarea name="txtTestString" id="txtTestString" cols="100" rows="8"></textarea>
</label>
</p>
<p>
Result:<br />
<textarea name="txtTestResult" id="txtTestResult" cols="100" rows="8"></textarea>
</p>
<p>
<label>
<input type="button" name="cmdTest" id="cmdTest" value="TEST" onclick="doTest();"/>
</label>
<label>
<input type="button" name="cmdMatch" id="cmdMatch" value="MATCH" onclick="findMatches();"/>
</label>
<label>
<input type="reset" name="cmdClearForm" id="cmdClearForm" value="RESET FORM" />
</label>
</p>
<p></p>
</form>
</body>
</html>
原文链接:https://www.f2er.com/regex/363043.html