Solved

Regular Expression using Javascript to match all words in any order

Posted on 2004-09-24
4
393 Views
Last Modified: 2008-02-01
Hi all,

I have a search string that I want to test against titles stored in another array.  for example
title = ["Handbook on Quality Assurance", "Water Quality Handbook", ...]
searchStr = "Handbook Quality"

I want to use regular expressions to test it each word is found at least once in the title but it doesn't matter in what order.  
I don't know how many words will be in the search string.
I've tried
new RegExp = ("("+searchStr.replace(" ", ")+.*(") + ")+");
(which would give /(Handbook)+.*(Quality)+/ in the above example)  
but this matches only title[0] and not title[1] (which I want to include).  

I was also thinking of using somewthing like /(Handbook|Quality) \1/  But if a title has the word Quality twice and not Handbook, it will still match (which I don't want)....

Because I have to loop through the title array (which is on the order of 200+ elements)  I don't want to have to do a sub loop for each of the search terms (which I don't how many there are) but can't think of any other way...

Any ideas???

Thanks,
Annie
0
Comment
Question by:andrea_lafleur
4 Comments
 
LVL 4

Expert Comment

by:aratani
ID: 12146194
Regular expressions generally work from left to right, so you have to somehow specify the order the values come in.

The best way for you to do this (with my knowledge of regular expressions) is to use a simple loop search (and loop searching doesn't take too much time, it might actually be faster than using regular expressions). Cause eventually all regular expression searches use loops anyway.

I can help you write a loop, but i don't think it is possible to do what you want using regular expressions unless some other expert can teach me something more about regular expressions that i didn't know :)).

Take care

AJ

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
ID: 12146565
AJ is true, but to use any RegExp here my example:

<script>
title = ["Handbook on Quality Assurance", "Water Quality Handbook", "More Quality Handbooks", "Last Quality Handbook"];
searchStr = "Handbook Quality"
 aSearch = searchStr.split(" ");
 for(i=0;i<aSearch.length;i++){
   aSearch[i] = new RegExp("\\b"+aSearch[i]+"\\b", "i");
 }

 for(i=0;i<title.length;i++){
   found = true;
   for(j=0;j<aSearch.length;j++){
     found &= (title[i].match(aSearch[j])!=null)
   }
   if(found) alert(title[i]);
 }

</script>


0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question