Which Regex parses search hints

My regular expression is terrible. I want to build an intelligent search, where I can give tips to the search engine about what to search for by what property.

Something like that:

Search for Entrance: Location: London

-> ["London"]

Search Entrance: Location: London, New York

-> ["London", "New York"]

Search entry: location: London tags: bar

-> ["London"]

-> ["Bar"]

Search Entrance: Location: London, New York Tags: Bar, Club

-> ["London", "New York"]

-> ["Bar", "Club"]

I wonder what the regular expression for user input should look like?

+4
source share
3 answers

What if you just split the line like this:

 > var s = "location: London, New York tags: Bar, Club"; > var splitted = s.split(/location: | tags: /); > splitted[1].split(', ') ["London", "New York"] > splitted[2].split(', ') ["Bar", "Club"] 
+1
source

I would use a bit of RegEx and Split.

 var input = 'location: London, New York tags: Bar, Club'; // example var arrays = input.split(/\s*\w+:\s*/); arrays.forEach(function (val, idx, arr) { arr[idx] = val.split(/,\s*/); }); 
+1
source

In the end, I used a different approach. Maybe I just don't get the other solutions correctly, but it seems to me that there is no way to say which target words belong to which key.

If location: London, New York tags: Bar, Club

will switch to tags: Bar, Club location: London, New York

how would I now, which target words belong to location and which tags ?

So, I did this:

 var locationMatch = searchString.match(/location:(((?!tags:)[-A-Za-z0-9, ])+)/); //locationMatch[1] are the target words for "location" var tagsMatch = searchString.match(/tags:(((?!location:)[-A-Za-z0-9, ])+)/); //tagsMatch[1] are the target words for "tags" 

I assume there is a simpler approach, but I am far from being regex pro.

0
source

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


All Articles