Regex javascript - matches multiple search terms, ignoring their order

I would like to find all matches of given strings (separated by spaces) in a string. (For example, the iTunes search box is running).

This, for example, as "ab de" and "de ab" will return true to "abcde" (also "bc ea" or any order should return true)

If I replaced the free space with a wild card, "ab * de" will return true to "abcde", but not "de * ab". [I use *, not Regex syntax for this explanation only]

I could not find a clean Regex solution for this. The only solution I could think of was to break the search word and run a few regular expressions.

Is it possible to find a pure expression in an expression that will cover all of these parameters?

+4
source share
7 answers

I'm sure you can come up with a regex to do what you want, but this may not be the most efficient approach.

For example, the regex pattern (?=.*bc)(?=.*e)(?=.*a) will match any string containing bc , e , and a .

 var isMatch = 'abcde'.match(/(?=.*bc)(?=.*e)(?=.*a)/) != null; // equals true var isMatch = 'bcde'.match(/(?=.*bc)(?=.*e)(?=.*a)/) != null; // equals false 

You can write a function to dynamically create an expression based on your search queries, but is the best way to accomplish what you do is another question.

+4
source

Returns true when all parts (separated by , or ' ' ) of the searchString are found in the text. Otherwise, false returned.

 filter(text, searchString) { const regexStr = '(?=.*' + searchString.split(/\,|\s/).join(')(?=.*') + ')'; const searchRegEx = new RegExp(regexStr, 'gi'); return text.match(searchRegEx) !== null; } 
+4
source

Try the following:

 var str = "your string"; str = str.split( " " ); for( var i = 0 ; i < str.length ; i++ ){ // your regexp match } 
+3
source

Alternation is independent of order:

 "abcde".match(/(ab|de)/g); // => ['ab', 'de'] "abcde".match(/(de|ab)/g); // => ['ab', 'de'] 

So, if you have a list of words that can be combined, you can build a regular expression with alternating on the fly like this:

 function regexForWordList(words) { return new RegExp('(' + words.join('|') + ')', 'g'); } 'abcde'.match(['a', 'e']); // => ['a', 'e'] 
+3
source

This is the script I'm using - it works with the same word searchStrings

 var what="test string with search cool word"; var searchString="search word"; var search = new RegExp(searchString, "gi"); // one-word searching // multiple search words if(searchString.indexOf(' ') != -1) { search=""; var words=searchString.split(" "); for(var i = 0; i < words.length; i++) { search+="(?=.*" + words[i] + ")"; } search = new RegExp(search + ".+", "gi"); } if(search.test(what)) { // found } else { // notfound } 
+2
source

I assume that you match words or parts of words. You want the search terms, separated by spaces, to limit the search results, and it seems that you intend to return only those records that contain all the words that the user provides. And you mean the wildcard character * to stand for 0 or more characters in the corresponding word.

For example, if a user searches for the words term1 term2 , you intend to return only those elements that have both the words term1 and term2 . If the user searches for the word term * , it will match any word starting with term .

There are suitable regular expressions that are equivalent to this search language and can be generated from it.

A simple example, the word term , can be claimed in regex by conversion to \bterm\b . But two or more words that must coincide in any order require statements of opinion. Using the extended syntax, the equivalent regular expression:

 (?= .* \b term1 \b ) (?= .* \b term2 \b ) 

An asterisk wildcard can be specified in a regular expression with a character class followed by an asterisk. The character class determines which letters you consider to be part of the word. For example, you may find that [A-Za-z0-9]* matches the score.

In short, you can be satisfied if you convert an expression, for example:

 foo ba* quux 

in

 (?= .* \b foo \b ) (?= .* \b ba[A-Za-z0-9]* \b ) (?= .* \b quux \b ) 

This is a simple search and replace question. But be careful to clear the input line to avoid injections by removing punctuation, etc.

+1
source

I think you can bark the wrong tree with RegEx. What you can see is the Levenshtein distance from the two input lines.

The Javascript implementation is implemented here and the usage example is here .

-1
source

Source: https://habr.com/ru/post/1390295/


All Articles