Skip to content

Instantly share code, notes, and snippets.

@leonlaser
Created August 20, 2020 06:21
Show Gist options
  • Save leonlaser/9feeb8ce63238b874e55cb99cc8a8112 to your computer and use it in GitHub Desktop.
Save leonlaser/9feeb8ce63238b874e55cb99cc8a8112 to your computer and use it in GitHub Desktop.
[Split text after x chars] #javascript #regex
// Source: https://stackoverflow.com/questions/27758483/split-text-string-at-closest-space-in-javascript
// Author: https://stackoverflow.com/users/1215106/%e2%84%a6mega
// If you are interested in just the first part, then use
var a = fulltext.match(/^.{47}\w*/)
// If you want to split the entire string to multiple substrings, then use
var a = fulltext.match(/.{47}\w*|.*/g);
// ...and if you wish substrings do not start with the word separator (such as space or comma) and prefer to include it with the previous match, then use
var a = fulltext.match(/.{47}\w*\W*|.*/g);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment