• Skip to content

Menu 1

  • Zug
  • Blog
  • Local
  • Printed
  • Projects
  • Railroad
  • Podcasts
  • Newsletter

Thomas Beutel Art

Menu 1

  • Zug
  • Blog
  • Local
  • Printed
  • Projects
  • Railroad
  • Podcasts
  • Newsletter

Follow us

Follow us on TwitterFollow us on InstagramFollow us on PinterestSubscribe to our Channel on YouTubeFollow us on SoundCloud
AuthorPostedbyThomason June 17, 2011

Get the first N words with Javascript

Here’s how I get the first 20 words using a regular expression and the Javascript replace function. I place an ellipsis at the end to indicate that the sentence was shortened:


var str1 = "Here is a sentence that just seems to go on and on. Here is a sentence that just seems to go on and on. Here is a sentence that just seems to go on and on.";
var str2 = str1.replace(/(([^\s]+\s\s*){20})(.*)/,"$1…");
document.write(str2);
// result:
// Here is a sentence that just seems to go on and on. Here is a sentence that just seems to …

♡

Posted in Uncategorized

1 Comment

  1. kk
    11 years ago Permalink

    Too good

Comments are closed.

Post navigation

Previous
Next

© 2025MINIMAL

Follow us

Follow us on TwitterFollow us on InstagramFollow us on PinterestSubscribe to our Channel on YouTubeFollow us on SoundCloud
x