1

I am trying to make a command like !find England Michanel to return list of people with a name Michael whose live in England.

let [country, name] = input.split(" ").slice(1);

This gives me exact data I want. But the problem is when the county name is more than one word like United States and also for person names like John Doe. If I apply my current logic will be like; country = United, name = States and will ignore the rest after that.

Is ther any way to retrieve country name person name with spaces correctly? Thanks

4
  • 1
    You'd have to do some kind of special parsing. You can check that the country name starts with "United" and then infer that the country name is 2 words, but it's a pretty crappy way of dealing with it. This is actually a pretty common problem. I've seen it solved by either dashing it (united-states) or underscores (united_states). Beyond that, you'll either have to do some kind of fuzzy searching, or rely on a search provider like Solr. Commented Feb 24, 2017 at 9:17
  • how does input look like? do you have control over it? please give an example of input Commented Feb 24, 2017 at 9:24
  • You need to be specific about the formation of the input first. Commented Feb 24, 2017 at 9:34
  • if input is "country name" why are you using .slice(1) and why are you using ! before your command/function name? Commented Feb 24, 2017 at 9:36

3 Answers 3

1

I assume you fetch your data from some kind of database? Ok, using a lot of assumption here:

1) You can simply NOT use the space as a separator (don't know if you have that option)

2) Moment you fetch your data, replace the space with e.a. '~'. ("United~States"). After you split and assign "United~States" to the array, replace the "~" again (or just search incl. ~)

Sign up to request clarification or add additional context in comments.

Comments

0

I you really want to support this and you dont mind extra latency:

Try all three:

!find United States John Doe:

Anyone called "Doe" in the country "United States John"

Anyone called "John Doe" in "United States" and

Anyone called "States John Doe" in "United"

Its extra work but answers the question.

Comments

0

Assuming that you can make minor adjustments to the commands.

To me the best way is to simply use flags and then apply regular expressions to get the full input after the flag.

Example !find -c United States -p Jonh Doe

All you have to do is use a regular expression to extract the values from -c until another flag, and basically the same for the name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.