I have the following string:
[SM_g]This[SM_h][SM_g]is[SM_h][SM_g]a[SM_h][SM_g]sentence.[SM_h][SM_l][SM_g]Here[SM_h][SM_g]is[SM_h][SM_g]another[SM_h][SM_g]sentence.[SM_h][SM_1]
And I can turn that string into this string, which I then display within a <p>
element:
This is a sentence.
Here is another sentence.
with the following code:
tokenResponseText_initial = "[SM_g]This[SM_h][SM_g]is[SM_h][SM_g]a[SM_h][SM_g]sentence.[SM_h][SM_l][SM_g]Here[SM_h][SM_g]is[SM_h][SM_g]another[SM_h][SM_g]sentence.[SM_h][SM_1]"
const newLineIndicator = "insert-double-new-line"
const tokenResponseText_fixedNewLines = tokenResponseText_initial.replace(/(\[SM_g].*?)(\[SM_h]\[SM_l])/g, "$1" + newLineIndicator + "$2");
const wordCompilationRegex = /\[SM_g](.*?)\[SM_h]/g;
var wordRegexResponse;
var summary = "";
do {
wordRegexResponse = wordCompilationRegex.exec(tokenResponseText_fixedNewLines);
if (wordRegexResponse) {
if (wordRegexResponse[1].includes(newLineIndicator)) {
summary += wordRegexResponse[1].replace(newLineIndicator, "") + "\n\n";
} else {
summary += wordRegexResponse[1] + " ";
}
}
} while (wordRegexResponse);
//The following is rough code,
someParagraphElement.innerHTML = summary;
p {
white-space: pre-line;
}
<p id="someParagraphElement"></p>
Where the paragraph element has the following attribute white-space: pre-line;
However, ideally, in order to create the double newline between the two sentences, I'd like to eliminate the use of newLineIndicator
and simply do this:
But, this second method does not work. The end result does not end having the double new lines even though when I print tokenResponseText_fixedNewLines
to the console, it seems like the double new lines have been inserted, like so:
[SM_g]This[SM_h][SM_g]is[SM_h][SM_g]a[SM_h][SM_g]sentence.
[SM_h][SM_l][SM_g]Here[SM_h][SM_g]is[SM_h][SM_g]another[SM_h][SM_g]sentence.[SM_h][SM_1]
tokenResponseText_initial = "[SM_g]This[SM_h][SM_g]is[SM_h][SM_g]a[SM_h][SM_g]sentence.[SM_h][SM_l][SM_g]Here[SM_h][SM_g]is[SM_h][SM_g]another[SM_h][SM_g]sentence.[SM_h][SM_1]"
const tokenResponseText_fixedNewLines = tokenResponseText_initial.replace(/(\[SM_g].*?)(\[SM_h]\[SM_l])/g, "$1\n\n$2");
const wordCompilationRegex = /\[SM_g](.*?)\[SM_h]/g;
var wordRegexResponse;
var summary = "";
do {
wordRegexResponse = wordCompilationRegex.exec(tokenResponseText_fixedNewLines);
if (wordRegexResponse) {
summary += wordRegexResponse[1] + " ";
}
} while (wordRegexResponse);
//The following is rough code,
someParagraphElement.innerHTML = summary;
p {
white-space: pre-line;
}
<p id="someParagraphElement"></p>
Why does the second method not work, although the first method does work? Does .*?
not capture new lines?