0

edit to clarify: oneword is a searchquery a user might use. I need to match both oneword, one word or word one with this regex


how to match a string with or without whitespace in any given order?

This is my current query /^(?=.*one)(?=.*word).*$/i credits: http://www.rubular.com/r/QFEfj9lMn3

So given the text oneword one word word one and the searchquery /^(?=.*one)(?=.*word).*$/i matches all three, but /^(?=.*oneword).*$/i only matches the first part of the text.

Are there better ways to fix this than by adding one optional character in front of every single letter?

thanks

Michi Eisele
  • 33
  • 1
  • 6
  • I don't understand your question. Can you please clearly state what result you would like from the regex match. – Tom Lord Nov 14 '17 at 09:55
  • Unless I'm missing something, couldn't you just write the two possibilities explicitly, like: `/one\s*word|word\s*one/`? – Tom Lord Nov 14 '17 at 09:57
  • I get the string from a searchQuery. A user might search for `one word` or `oneword`. I would like to create a regex query that matches for both search strings all three variations of `oneword one word word one ` So the problem is not the word order but matching it if the searchquery is either written with a whitespace or without (the other way around is rather trivial) – Michi Eisele Nov 14 '17 at 09:57
  • @TomLord thanks for you help, but how would I know where to place the optional character in oneword? Other examplse might be match `stackoverflow` to both `stack overflow` and `stackoverflow` – Michi Eisele Nov 14 '17 at 10:01
  • 1
    You'll need to have **some** way of knowing what the two words are. It would not be practical to perform a search like: `s ?t ?a ?c ?k ?o ?v ?e ?r ?f ?l ?o ?w` – Tom Lord Nov 14 '17 at 10:18
  • 1
    Unless you have some practical way to predict how to split the word, I'd suggest using ElasticSearch instead of regex. – Tom Lord Nov 14 '17 at 10:19

1 Answers1

0

Try this simple regex generation

function showMatches(sampleInput, searchInput) {
  console.log(sampleInput, searchInput);
  if (sampleInput.length && searchInput.length) {
    var regexStr = searchInput.split(" ").join("\\\s*") + "|" + searchInput.split(" ").reverse().join("\\\s*");
    var regex = new RegExp(regexStr, "gi");
    //console.log(regex);
    console.log(sampleInput.match(regex))
  }
}
showMatches("one word word one oneword", "one word");

Explanation

  • Split the input by space to get words.
  • Join them by \\\s*
  • Repeat the same for reversed words
  • Combine both "|"

Note - You may also want to escape special regular expression characters from search inputs using an approach shown here.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Thank you for your well explained answer, but I have no idea where to split these two words. `searchInput` might be `oneword` which does not match `one word` – Michi Eisele Nov 14 '17 at 13:42
  • You can't, unless you have a dictionary of all words and you want to first get a match of possible words. Match `oneword` from input to `word one` in sample involves *natural language processing* which is far too broad. – gurvinder372 Nov 14 '17 at 13:46