Javascript Regular Expression Testing Utility

JugglerDrummer 0 Tallied Votes 211 Views Share

Make regex creation easier! Enter regex, test string, select from various function to run, then see the output.

[code]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>Javascript Regular Expression Testing Utility</title>
		<style type="text/css">
			body {
				background-color: #eeeeee;
			}
			#header h1, #header h2 {
				 text-align: center; 
				 }
			label {
				clear: left;
				float: left;
				margin: .5em ;
				width: 5em;
			}
			input {
				float: left;
				margin: .5em;
			}
			#btnsubmit {
				clear: left;
				margin: 10px;
			}
			#output {
				clear: left;
				margin: 10px;
				font-size: 1.5em;
			}
		</style>
		<script type="text/javascript">
			function process(){
				var regexvalue = document.getElementById("txtregex").value;
				var flags = document.getElementById("txtflags").value;
				var string = document.getElementById("txtstring").value;
				var outputdiv = document.getElementById("output");
				var outpt, checktype, regex, keepgoing=true;
				if ( regex === "" || string === "" )
					output="please fill in all required fields";
				if (flags.match(/[^igm]/)) {
					output = "invalid flags";
					keepgoing=false;
				}	
					
				if (keepgoing) {
					var radios = document.f1.radtesttype;
					for (var i = 0; i < radios.length; i++) {
						if (radios[i].checked) {
							checktype = radios[i].value;
							break;
						}
					}
					if (flags) 
						regex = RegExp(regexvalue, flags);
					else 
						regex = RegExp(regexvalue);
					
					switch (checktype) {
						case "search":
							output = string.search(regex);
							break;
						case "replace":
							var replacetext=document.getElementById("txtreplace").value;
							if (replacetext)
							{
								output = string.replace(regex,replacetext);
							}
							else
							{
								output="Please fill in the 'replace' text-field"
							}
							break;
						case "match":
							output = string.match(regex);
							break;
						case "split":
							output = string.split(regex);
							break;
						case "exec":
							output = regex.exec(string);
							break;
						case "test":
							output = regex.test(string);
							break;
						default:
						output="Please pick a search type";
					}
					
				}//end keepgoing if
				outputdiv.innerHTML=output;
				
			}
		</script>
	</head>
	<body>
		<div id="header">
			<h1>Javascript Regular Expression Testing Utility</h1>
			<h2>Made by CrazyJugglerDrummer</h2>
		</div>
		<div id="content">
			<form action="" name="f1">
				<label>Test String:</label><textarea rows="5" cols="90" "name="txtstring" id="txtstring"></textarea>
				<label>Regex:</label><input type="text" name="txtregex" id="txtregex" size="50" />
				<label class="flaginput">Flags:</label><input type="text" name="txtflags" id="txtflags" />
				<label>Replace text:</label><input type="text" name="txtreplace" id="txtreplace" size="50" />
				
				<input type="button" name="btnsubmit" id="btnsubmit" value="check regex" onclick="process()"/>
				
				<label>search</label><input type="radio" name="radtesttype" value="search" />
				<label>replace</label><input type="radio" name="radtesttype" value="replace" />
				<label>match</label><input type="radio" name="radtesttype" value="match" />
				<label>split</label><input type="radio" name="radtesttype" value="split" />
				<label>exec</label><input type="radio" name="radtesttype" value="exec" />
				<label>test</label><input type="radio" name="radtesttype" value="test" />
			</form>
			<div id="output"></div>
		</div>
	</body>
</html>
[/code]
JugglerDrummer 0 Junior Poster

If you have any comments, suggestions, or made any improvements, please post them! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.