2

With out using jquery, what is the easiest way to do this? If I have a string containing 600 words, and I want only the first 100 or so words and then trail off with: ... what is the easiest way to do this?

I found: replace(/(([^\s]+\s\s*){40})(.*)/,"$1…"); But I don' understand regex enough to know if this is right or not.

TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • Did you test that code? – Hayden Schiff Mar 18 '15 at 16:02
  • 4
    `str.split(/\s+/).slice(0, 100).join(' ') + '...'` – dfsq Mar 18 '15 at 16:06
  • @dfsq, that's slicing on spaces, shouldn't it be better to be counting words by using word bounderies? – Mouser Mar 18 '15 at 16:07
  • @Mouser strictly speaking that would cause trouble with hyphenated words. I don't think the exactness of 40 is of utmost importance, but I would think it's nicer not to break off in the midst of a hyphenated word which you happen with word bounderies. – asontu Mar 18 '15 at 16:09
  • `^(?:\b\w+\b[\s\r\n]*){1,100}$` (credit: http://stackoverflow.com/questions/557695/limit-the-number-of-words-in-a-response-with-a-regular-expression ) – bardzusny Mar 18 '15 at 16:09

2 Answers2

4

@dfsq's solution is a nice straight forward one, but in the interest of learning :)

To try and understand regexes, I would advice looking at the flow-chart-alike visualisation that Debuggex gives you, and experiment with substitution on Regex101. (links contain the regex unaltered from your question)

I would make some small modifications:

  1. The . doesn't match new-lines, one way to do that is to match [\s\S] in stead, which matches absolutely every character
  2. This: [^\s]+\s\s* can be untangled and optimized to \S+\s+, but I would turn it around and make it \s*\S+ so that the ... comes after the last word without a space in between.

Which would result in this:

((\s*\S+){40})([\s\S]*)

Regular expression visualization

Regex101 substitution in action

asontu
  • 4,548
  • 1
  • 21
  • 29
1

That's quite simple I guess (I don't know if this is the best way) !

for(var i=1;i <= numberOfWords;i++) {
    var word = mystr.substr(0,mystr.indexOf(' ')+1).trim();
    arrayOfWords.push(word);
    mystr = mystr.substr(mystr.indexOf(' '),mystr.length-1).trim();
}

Try for yourself: http://jsfiddle.net/fourat05/w386mxaa/

Fourat
  • 2,366
  • 4
  • 38
  • 53