Hi,

I need to search for strings and retrieve it and display. Using Regular Expression i tried but my need is that :

For example if i need to search for country names when the use enters only first 2 or 3 letters it should return back the names starting with that 3.

Let it be Austria,Australia.

As of now i used ^$ to handle multiple lines in a string. But only when a use enters Austria it retrieves it.

I need like when a user enters Aus or aus it should return back Australia and Austria.

Whether it is possible using REgular Expressions in Javascript?

Hi av1,

Here's how you can do it, using RegExp .
Try to input a single word in "bu", bug, or any specific word containing (bu) and it will search all the string's inside the div and output the closest match.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>Search String Using Regular Expression</title>
<style type="text/css">
/* <![CDATA[ */
ol, div#show {
   font : bold 80%/1.6 "Trebuchet MS", "Bernard MT Condensed", Tahoma, Verdana;
   color : #708090;
   margin : 1em 0 1em 1.500em;
}
   
/* ]]> */
</style>
<script type="text/javascript">
// <![CDATA[
var myString, str, get, stringMatch, searchText;
var i, x;
String.prototype.get = function(PATTERN) {
   str = new RegExp();
   str.compile( PATTERN + "(\\s|\\w+)", "ig");
   return this.match( str );
};
searchText = function() {
   div = (( document.getElementById ) ? document.getElementById("show") : document.all.show );
   myString = (( document.getElementById ) ? document.getElementById("text").innerText : document.all.text.innerText );
   myQuery = myString.get(( document.getElementById ) ? (( document.getElementById("txt").value === "" ) ? alert("This field cannot be empty") : document.getElementById("txt").value ) : (( document.all.txt.value === "" ) ? alert("This field cannot be empty") : document.all.txt.value ));
   if ( myQuery !== null ) {
      try {
      div.innerHTML = "";
      ol = document.createElement("ol");
         for ( i = 0; i < myQuery.length; i++ ) { 
         li = document.createElement("li");
         stringMatch = document.createTextNode( myQuery[ i ] );
      li.appendChild( stringMatch );
      ol.appendChild( li );
      }
      div.appendChild( ol );
      } catch( e ) {
         for ( x = 0; x < myQuery.length; x++ ) {
      div.innerHTML += ( x + 1 )  + ". " + myQuery[ x ] + "<br />\n";
         }
      }
   } return false;         
};
// ]]>
</script>
</head>
<body>
<div id="text">Please help me to identify ( buzz jazz bull bullets buns bug ) the words with an instance of "bu"!</div>
<div id="show"></div>

<p><label for="txt">Enter some string: <input type="text" id="txt" name="txt" value="" /></label><button id="btn" name="btn" onclick="searchText();">Search Text</button></p>
</body>
</html>

hope it get's what you need...

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.