The following demo features a function that will break text after a colon on a given string. This function will render the text in HTML or return the edited text as a string value to used elsewhere.
/**
* colonBreak(data, DOM = false)
=------------------------------=
* Edit text in a particular format pattern:
* Insert a line break after any colon.
=------------------------------------------=
* @param {string} data - There are two forms of strings that can be accepted:
* 1. a literal string or template literal
* 2. the selector of a DOM element that has the text
* @param {boolean} DOM - If undefined it defaults to false
* true: data represents the selector of a DOM element
* text of DOM element will be formatted
* false: data is the string to be formatted
=----------------------------------------------------------------------------=
* @return {string} if DOM is false, a formatted string will be returned.
* or
* {undefined} if DOM is true, undefined will be returned and the DOM
* element text content will be formatted
*/
Explanation
When dealing with the text of DOM elements, HTML formats text by default so that multiple whitespaces and line-breaks are normalized. .innerText
property is used so that the whitespaces and line-breaks are preserved (\n
is not ignored). .innerText
or .textContent
can be used to extract the text but it's important to apply .innerText
to render the formatted string back into the DOM element.
string = node.innerText;
The heart of this function consists of .split()
method witch splits the string by a RegExp delimiter into an array of strings:
string.split(/([\w]+:)\s*/)...
The RegExp delimiter instructions are as follows:
(
...)
capture the match within the parenthesis and use it as a replacement
[
...]
match the literal and/or class of each character and/or meta-character
\w
meta-character that matches any alphanumeric character (including underscores)
+
quantifier indicating match of one or more consecutive instances of the match preceding it
:
literal colon
\s
meta-character that matches a whitespace
*
quantifier as +
but matches zero or more
Last method used is .join()
which will join all the strings of an array into one complete string by a given delimiter:
let formatted = string.split(/([\w]+:)\s*/).join('\n')
Demo
Details are commented in demo
/*
There are two examples of input:
1. A <p> with text (located in HTML window of demo)
2. A string assigned to the variable dataString (below)
(NOTE: colonBreak() normalizes spacing as well)
*/
const dataString = `keyX: xXxXxXXxXxXx keyY:yYyYyY YyYyYy keyZ: zZ zZ zZ Zz Zz Zz`;
/*
See documentation in post about the parameters and return
*/
function colonBreak(data, DOM = false) {
let node, string;
// if DOM parameter is true...
if (DOM) {
/*
- Reference the DOM element designated by param data
- Get the textNode within the DOM element
(NOTE: .innerText preserves whitespaces)
- Overwrite textNode with the formatted text
(NOTE: See post on how text is formatted)
*/
node = document.querySelector(data);
string = node.textContent;
node.innerText = string.split(/([\w]+:)\s*/).join(`\n`);
} else {
/*
...otherwise just return the formatted text
(NOTE: See post on how text is formatted)
*/
return data.split(/([\w]+:)\s*/).join(`\n`);
}
return false;
}
console.log(colonBreak(dataString));
colonBreak('p', true);
.as-console-wrapper {
width: 350px;
min-height: 100%;
margin-left: 45%;
}
<p>keyX: xXxXxXXxXxXx keyY:yYyYyY YyYyYy keyZ: zZ zZ zZ Zz Zz Zz</p>
` instead of `\n`? – Nick Parsons Jan 29 '20 at 02:14
` instead of a `\n`. – Robby Cornelissen Jan 29 '20 at 02:14
SOME OTHER WORDS. – Kirk Ross Jan 29 '20 at 02:17