mercredi 6 mai 2015

Regular expression to create multiple word fragments based off the same words

Let's say I have the following string:

var str = "I like barbeque at dawn";

I want pairs of all words which are separated by a space. This can be achieved via the following regular expression:

  var regex = /[a-zA-Z]+ [a-zA-Z]+/g;
  str.match(regex);

This results in:

["I like", "barbeque at"]

But what if I want ALL permutations of the pairs? The regular expression fails, because it only matches any given word onces. For example, this is what I want:

["I like", "like barbeque", "barbeque at", "at dawn"]

I know I can use the recursive backtracking pattern to generate permutations. Do regular expressions have the power to create these types of pairs for me?

Aucun commentaire:

Enregistrer un commentaire