?
Solved

Regular Expression using Javascript to match all words in any order

Posted on 2004-09-24
4
Medium Priority
?
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

764 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