Regular Expressions In Use

[Total: 0    Average: 0/5]

This shows how to use the regular expressions to find a pattern in a string.

Body Code:

<form name=exf1>
<BR> String: <input type=text name=t1 value="A red shoe and a blue shoe make a purple shoe?">
<BR> Search For: <input type=text name=t2 value="shoe" onfocus="resetIt()">
<BR> Result[0]: <input type=text name=t3 value="?">
<BR> Result.Index: <input type=text name=t4 value="?">
<BR><input type=button value="run search" onclick="searchMe()">
</form>
<script type=text/javascript>
function resetIt() {
document.exf1.t3.value='?';
document.exf1.t4.value='?';
}
function searchMe() {
  var result;
  var returnvalue = "";
  var text = document.exf1.t1.value;
  var pattern = eval("/"+document.exf1.t2.value+"/i");
  var result = pattern.exec(text);
  if (!result) return;
  document.exf1.t3.value=result[0];
  document.exf1.t4.value=result.index;
}
</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *