3 Replies - 2349 Views - Last Post: 05 November 2011 - 06:48 AM

#1 jeansymolanza   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 20-February 08

Javascript search in array or string for individual characters

Posted 03 November 2011 - 02:14 AM

I'm writing a piece of JS code that returns a result of true if the pattern appears in string as a substring (case sensitive) but would like to extend its functionality to returns true if all the individual characters of pattern appear in string (regardless of order).

For example:

This is what the program currently does:

match1("adipisci","pis") returns true


Whereas I would now like it to do this:

match1("adipisci","sciip") returns true
match2("adipisci","sciipx") returns false because x does not exist in variable
match3["adipisci","adipisci"] returns true in array 1 and 2 if "sciip" is searched 
match4["adipisci","adipiscix"] returns false in array 1 and true in array 2 if "sciipx" is searched 


I am having difficulty implementing this into my code... this is what I've done so far:

var pages=[
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
  "Nulla imperdiet laoreet neque.",
  "Praesent at gravida nisl. Quisque tincidunt, est ac porta malesuada, augue lorem posuere lacus, vitae commodo purus nunc et lacus."
  ];
var search_term = prompt('Type in search term: ','Search word');
// ask which search term the user wants to search for with default being 'Search Term' 
function find_s(w) {
    var indexes = [0,0,0] // create an array  to hold pages where search term found
    var iii = 0 // create variable to increment the number of finds in each page
    for (var ii=0;ii<pages.length;ii++) {
    // for each page in the array
        for (var i=0;i<pages[ii].length;i++) {
        // for each character in the chosen page
            if (pages[ii].substr(i,w.length).toLowerCase()==w.substr(0,w.length).toLowerCase()) {
            // check to see if the search term is there ignoring case
                iii++;
                // increment number of times the search term in found
                while(pages[ii].substr(i,1)!=" ") {
                    // move to the next word but checking for spaces
                    i++;
                    }
            }
        }
        indexes[ii]=iii;
        // update the number of times the search term is found in that page
        iii=0;
        // reset counter for next page search
    }
    return (w + " was found in this may times in each page " + indexes);
    // let the user know the result
}
alert (find_s(search_term));


Is This A Good Question/Topic? 0
  • +

Replies To: Javascript search in array or string for individual characters

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Javascript search in array or string for individual characters

Posted 03 November 2011 - 05:50 AM

For that kind of functionality without having to code a possibly lengthy and complex algorithm I'd suggest using Regular Expressions. This link (JavascriptKit: Regular Expressions - Introduction) will be able to assist you in getting started.

It's as simple as adding [ ] around each term you would like to search for then converting the string to a regular expression.

Example
<html>
<head>
<script type="text/javascript">
var content = 'This is just a sample text. There is no need to be alarmed at this';
//we'll be looking for each word in our search array
var search = ['alarm', 'sample'];

//we will parse our search array via a self-invoking function
//this function will return regex_buffer
//you could make a separate function, call it and you'll still
//get the same results
var regex_string = (function(params)
{
	var regex_buffer = '';
	for(x in params) {
		//enclose each search parameter with []
		regex_buffer += '[' + params[x] + ']'
	}
	return regex_buffer
})(search);

//create our regular expression
var regex = new RegExp(regex_string);

//perform a search
//-1 is returned if nothing is found
if(content.search(regex) > 0) {
	alert('Match(es) found');
}
else {
	alert('Sorry no matches found');
}
</script>
</head>
</html>



Links you should read
Javascript RegExp Object
Regular Expression Patterns
Was This Post Helpful? 2
  • +
  • -

#3 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: Javascript search in array or string for individual characters

Posted 03 November 2011 - 07:56 AM

You could compress it a bit more by changing this...
var regex_string = (function(params) {
  return '['+params.join('][')+']';
})(search);


:bananaman:
Was This Post Helpful? 2
  • +
  • -

#4 jeansymolanza   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 20-February 08

Re: Javascript search in array or string for individual characters

Posted 05 November 2011 - 06:48 AM

Thanks for the help guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1