Adding a line break after the full title bar in a line

I am currently working on a project in which the API will hide the description, which is formatted as follows:

IT'S ALSO This is a paragraph. This is another paragraph.

And the result I'm trying to get is

IT'S THE SAME

This is a paragraph.

This is another paragraph.

I played with some regular expressions, but struggled to get the desired result, my current attempt is to add a line break at the end of each top-level letter / word.

var myString = "THIS IS A TITLE This is a paragraph of random text. This is another paragraph of random text.";
var myStringFormatted = myString.replace(/([A-Z]+)/g, ",$1").replace(/,/g," <br />").split(",");

Is it possible to get the desired result? (jsfiddle example: https://jsfiddle.net/srwwpxh4/1/ )

+4
source share
1 answer

.

, , , .

var r = /\W+(?=[A-Z][a-z])/g,
    s = 'THIS IS A TITLE This is a paraGraph. This is another paragraph.';

console.log(s.replace(r, '$&\n'));
Hide result
+7

Source: https://habr.com/ru/post/1659815/


All Articles