Javascript regex, match all words before length limit

I'm trying to split a long title into different small parts. Each small part shouldn't exceed the limit of 10 characters and I cannot find a solution... Currently, I'm trying with /(.{10}\S*)\s/ but testing with TESLA AND MICROSOFT AND ANOTHER WORD it returns also the word that is included in the limit (TESLA AND MICROSOFT AND ANOTHER WORD) and I don't want it... I mean, the expected result should be TESLA AND that has less than 10 characters... Is there a simple way to do it?

EDIT: What I want to do, it to split a single-line text into multiple lines, and the line length shouldn't be greater than 10 characters if there are multiple words. If a single word is longer than 10 characters, should return the full word. eg. TESLA AND MICROSOFT ANDANOTHERLONGWORD AND OTHER LONGWORDS

this text should be:

TESLA AND
MICROSOFT
ANDANOTHERLONGWORD
AND OTHER
LONGWORDS

Upvotes: 2

Views: 569

Answers (2)

Hao Wu
Hao Wu

Reputation: 20669

You may try this one:

/(?!\s)(?:[A-Z\s-]{0,9}[A-Z]|[A-Z-]+)\b/mg
  • (?!\s) make sure the first character is not a white space
  • (?:...) non-capture group
  • [A-Z\s-]{0,9}[A-Z-] any [A-Z-] character and spaces at length of 0-9, ends by [A-Z-](eliminates spaces for both ends)
  • |[A-Z-]+ or a long word of any length with no spaces in between
  • \b with a word boundary ahead

Check the test results

const text = 'S-TESLA INSTALACOES INDUSTRIAIS EIRELI';

const regex = /(?!\s)(?:[A-Z\s-]{0,9}[A-Z]|[A-Z-]+)\b/mg;

console.log(text.match(regex));

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163277

It you want to use split, you can use a capture group to keep what is captured after using split.

As split might leave empty entries, you can remove them from the final result, and optionally use trim to remove the trailing spaces.

(?:\s|^)(\S.{0,9}|\S+)(?!\S)
  • (?:\s|^) Match either a whitspace char or assert the start of the string
  • ( Capture group 1
    • \S.{0,9}|S+ Match a non whitespace char and 0-9 times any char or match 1 or more non whitespace chars.
  • ) Close group 1
  • (?!\S) Assert a whitespace boundary to the right.

const regex = /(?:\s|^)(\S.{0,9}|\S+)(?!\S)/;
[
  "TESLA AND MICROSOFT ANDANOTHERLONGWORD AND OTHER LONGWORDS",
  "I LOVE REGULAR EXPRESSIONS",
  "I",
  "TESLA AND MICROSOFT ANDANOTHERLONGWORD ANDANOTHERLONGWORD ANDANOTHERLONGWORD AND OTHER LONGWORDS"
].forEach(str => console.log(str
  .split(regex)
  .map(s => s.trim())
  .filter(Boolean)));

Upvotes: 3

Related Questions