Solved

Regular Expression using Javascript to match all words in any order

Posted on 2004-09-24
4
391 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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now