Skip to content

textSplit

Splits a given sentence into an array of words.

Each word keeps the whitespace that follows it (shown as ␣), so joining the tokens back together reproduces the original sentence exactly. That is what lets textWrap measure and re-flow text without losing spacing.

import {textSplit} from "@d3plus/text";

textSplit("The quick brown fox");
textSplit("one  two   three");

Chinese, Japanese, and Korean characters and emoji have no spaces to break on, so each one becomes its own token and a line can wrap between any pair of them.

import {textSplit} from "@d3plus/text";

textSplit("日本語のテキスト");
textSplit("party time 🎉🎈");

Words of eight or more lowercase letters are split into syllables, each ending in a soft hyphen (‧ here, \u00ad in the snippet). A line can break at any of them and the hyphen only becomes visible when it does.

import {textSplit} from "@d3plus/text";

textSplit("internationalization standards");
textSplit("an extraordinarily unremarkable sentence");

Parameters

import {textSplit} from "@d3plus/text";
sentencerequiredstring

The sentence to split into words.