0

I need to match a group of words and then match another group within those found. How can I do it with regexp? What is the general way for doing this?

For example I need to find words longer than 8 letters and then split all those words by groups of 4 letters, so that I can insert a dash or any other symbol with javascript.

So if my text is:

A full Reference & Help is available in the Library, or watch the video Tutorial.

my regexp must match

['Refe', 'renc', 'avai', 'labl', 'Tuto', 'rial']

I know that I can select long words with something like /[a-z]{8,}/ig and then I need to apply somethig like /[a-z]{3}/ig. So how do I combine the two? If I just do /([a-z]{8,})[a-z]{4}/ig it doesn't give me the right match.

artemean
  • 805
  • 1
  • 10
  • 24

1 Answers1

1

For this particular case, you could use lookaround:

/(?<=[a-z]{4})[a-z]{4}|[a-z]{4}(?=[a-z]{4})/ig

But JS doesn't have lookbehind.

In general, you would just match the 8-or-more-letter words, and then do your custom processing to each of the matches in a second step:

str.replace(/[a-z]{8,}/ig, function(match) {
    return match.replace(/[a-z]{4}(?=[a-z])/ig, "$&-"); // or whatever
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @torazaburo: Ah, right, we'd need lookbehind as well – Bergi Aug 02 '15 at 19:06
  • `return match.replace(/^([a-z]{4})([a-z]{4})\w*/ig, "$1-$2");` – Vidul Aug 02 '15 at 19:16
  • If the word is exactly 8 letter, it will - to the end: A full Refe-renc-e & Help is avai-labl-e in the Library, or watch the video Tuto-rial-." – Ori Drori Aug 02 '15 at 19:16
  • @OriDrori: Then use [this approach](http://stackoverflow.com/q/7033639/1048572) and `.join("-")`. Or the lookahead as in the updated answer. Or whatever :-) – Bergi Aug 02 '15 at 19:22
  • @Bergi, thanks! I tested your regexp for php and it works exactly as it should. Too bad it doesn't work for JS... – artemean Aug 02 '15 at 19:35