正则表达式HTML版工具

<HTML>
<HEAD>
<TITLE>Regular Expression Test Page</TITLE>

<!--- Some styles to make the page look nice --->
<STYLE>
  BODY {font-family:arial;font-size:12px}
  INPUT {font-size:11px}
  TEXTAREA {font-size:11px}
  TH {background:#8888FF;color:white;text-align:left}
  TD {background:#CCCC99;color:black;font-size:12px}
  TD.RowA {background:#CCCC99}
  TD.RowB {background:#EEEEBB}
  .header {font-weight:bold}
</STYLE>

<SCRIPT LANGUAGE="JavaScript">
// Turn off fields used only by replace
function hideReplaceFields() {
  document.getElementById('RegExReplace').disabled=true;
  document.getElementById('replaceheader').disabled=true;
}

// Turn on fields used only by replace
function showReplaceFields() {
  document.getElementById('RegExReplace').disabled=false;
  document.getElementById('replaceheader').disabled=false;
}

// Perform a find
function processRegexFind(text, regex, flags) {
	var reg = new RegExp(regex, flags);
	var lastIdx = -1;
	var iCount = 0;
	var result = "";
	var output = '<DIV STYLE="height:200px;overflow-y:auto;width:550">' +
				 '<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="0" WIDTH="550">' +
				 '<TR><TH WIDTH="*">Match</TH><TH WIDTH="50">Position</TH><TH WIDTH="50">Length</TH></TR>';

	// Loop as long as have matches
	while (lastIdx != 0)
	{
		// Do it
		var mtch = reg.exec(text);
		
		// Check if got one
		if (reg.lastIndex != 0)
		{
			// Yep, increment counter
			iCount++;

			if (iCount % 2)
				style = "RowA";
			else
				style = "RowB";
			// Write output
			output += '<TR CLASS="' + style + '"><TD>' +
						RegExp.lastMatch + "</TD><TD>" +
						(reg.lastIndex-RegExp.lastMatch.length) + "</TD><TD>" +
						RegExp.lastMatch.length + "</TD></TR>";
		}
		
		lastIdx = reg.lastIndex;
	}
	
	output += "</TABLE>";

	// Build result
	if (iCount != 0)
		result = "Matches Found: " + iCount+ "<BR>" + output;
	else
		result = "No matches";		
		
	return result;
}

// Process a replace
function processRegexReplace(text, regexfind, regexreplace, flags) {
	// Define regex
	var re = new RegExp (regexfind, flags) ;
	// Do it
	var newstr = text.replace(re, regexreplace) ;
	// Generate output
	var result = '<DIV STYLE="height:200px;overflow-y:auto;width:550">' +
				 '<TABLE BORDER="0" CELLPADDING="2" CELLSPACING="0" WIDTH="550">' +
				 '<TR><TH>New Text</TH></TR><TR><TD>' +
				 newstr + '</TD></TR>';

return result;
}

// Process entry point
function processRegex(form) {
	var output="";
	var flags;
	if (form.CaseSensitive.checked)
		flags = "g";
	else
		flags = "gi";

	// What to do?
	if (form.OperationFind.checked) {
		output=processRegexFind(form.SearchText.value, form.RegEx.value, flags);
	}
	else if (form.OperationReplace.checked) {
		output=processRegexReplace(form.SearchText.value, form.RegEx.value, form.RegExReplace.value, flags);
	}

	document.getElementById('output').innerHTML=output;
		
	return false;
}
</SCRIPT>
</HEAD>

<BODY>

<FORM NAME="tester" ACTION="" METHOD="post" onSubmit="processRegex(this);return false">
    
<TABLE BORDER="1" CELLPADDING="4" CELLSPACING="0" WIDTH="550">
  <TR>
    <TH CLASS="Dialog">Regular Expression Tester</TH>
  </TR>
  <TR> 
    <TD CLASS="Dialog"> 

    <!--- Text input for the regular expression itself --->
    <SPAN CLASS="header">Enter a regular expression:</SPAN><BR>
    <INPUT NAME="RegEx" TYPE="Text" SIZE="65" STYLE="font-size:13px">

    <!--- Checkbox to control case-sensitivity --->
    <INPUT TYPE="Checkbox" NAME="CaseSensitive" ID="CaseSensitive" VALUE="Yes">
    <LABEL FOR="CaseSensitive">Case sensitive</LABEL>
    <BR>

    <!--- Radio buttons to display find vs. replace --->
    <INPUT TYPE="Radio" NAME="Operation" ID="OperationFind" VALUE="find" CHECKED onClick="hideReplaceFields()">
    <LABEL FOR="OperationFind">Find</LABEL>
    <INPUT TYPE="Radio" NAME="Operation" ID="OperationReplace" VALUE="replace" onClick="showReplaceFields()">
    <LABEL FOR="OperationReplace">Replace</LABEL>
    <BR>

    <!--- Text input for the replace regular expression --->
    <SPAN CLASS="header" ID="replaceheader">Enter the replace regular expression:</SPAN><BR>
    <INPUT ID="RegExReplace" NAME="RegExReplace" TYPE="Text" SIZE="65" STYLE="font-size:13px">
    <BR><BR>

    <!--- Textarea where user can type the text to search --->  
    <SPAN CLASS="header">And the text you wish to search:</SPAN><BR>
    <TEXTAREA NAME="SearchText" WRAP="off" COLS="70" ROWS="6"></TEXTAREA>
    <BR>

    <!--- Submit button to start the search --->  
    <INPUT NAME="Submit" TYPE="Submit" STYLE="font-weight:bold" VALUE="Match Now"></TD>
  </TR>
</TABLE>
  
</FORM>

<!-- Display any reults here --->
<SPAN id="output"></SPAN> 

<!-- Default to find --->
<SCRIPT LANGUAGE="JavaScript">
hideReplaceFields();
</SCRIPT>

</BODY>
</HTML>


新建文本文档,把上面代码拷贝进去,然后更改后缀名为.html(此工具引用了www.forta.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值