Solved

Regular Expression using Javascript to match all words in any order

Posted on 2004-09-24
4
396 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

734 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