0

I need to return words from an string that are over a certain length and do this on 1 line of code.

Say I need to return all words over 2 chars in length...

So far I have...

const wordsOver2Chars = str => str.match(/\w+\s+(.{2,})/g);

console.log(
  wordsOver2Chars('w gh w qwe regh aerguh eriygarew  hw whio wh w')
);

This does not work.

str.match(/\w+\s+/g) will return an array of words but I cannot figure out how to add in the length limiter as well.

Using split(' ').match(\regExp) errors.

adiga
  • 34,372
  • 9
  • 61
  • 83
daktau
  • 633
  • 1
  • 7
  • 17
  • 1
    https://stackoverflow.com/questions/9043820/regex-to-match-words-of-a-certain-length – epascarello Jan 10 '22 at 14:21
  • 2
    "and do this on 1 line of code" is generally a bad requirement - code golfing and real world production code have nothing in common. That aside, why not `/\w{3,}/g`? – ASDFGerte Jan 10 '22 at 14:21
  • I had not seen 'code golf' before, but I'm now prepared to lose hours of my life to it. – paddyfields Jan 10 '22 at 14:42

3 Answers3

3

Use .split then .filter.

console.log('w gh w qwe regh aerguh eriygarew  hw whio wh w'.split(" ").filter(word => word.length > 2))
sean-7777
  • 700
  • 4
  • 13
  • I like this the best, much clearer than a regex, easy to see what it does at a glance. I'd only use regular expressions for this task if I was worried about performance. – Arthur Boucher Jan 10 '22 at 14:26
  • The question isn't clear about what a "word" is, and what else may be there, but think about strings with punctuation, tabs, etc, and it quickly becomes difficult without regex. As it's unclear, this may be just fine though. – ASDFGerte Jan 10 '22 at 14:36
1

The \w metacharacter matches word characters. When you add a + sign to it, you are implying that you want a word character chain of length at least 1, if you add another \w in front of it, you get min length of 2. And so on and so forth.

const wordsOver2Chars = str => str.match(/\w\w\w+/g);

console.log(wordsOver2Chars('w gh w qwe regh aerguh eriygarew  hw whio wh w'));

This is probably the easiest to understand approach, you are matching a single wordcharacter, followed by another one, and then followed by a 1+ chain.

If you want to be technically correct you can use curly brackets to define the number of elements, (3 being min, and empty after a comma meaning not defined max)

const wordsOver2Chars = str => str.match(/\w{3,}/g);

console.log(wordsOver2Chars('w gh w qwe regh aerguh eriygarew  hw whio wh w'));
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
0

Do you need to use regex? The easiest way would be to do

const wordsOver2Chars = s => s.split(' ').filter(w => w.length > 2).join(' ');

Split the string at ' ', then filter the resulting array to only contain words with length > 2 and join them again.

  • This approach might not work with some edge cases and depending on what should be considered a "word". For example, the string `'I, John Doe, hereby declare that...'` would include in the returned array the string `'I,'` which might or might not be what was intended. – secan Jan 10 '22 at 14:59