user992731
user992731

Reputation: 3520

Force line break every x amount of characters but preserve words from being clipped

I am getting back a long string of words and need to add some line breaks to make it readable.

Using the method below I am able to add breaks, but it cuts off words in various places.

JsFiddle: https://jsfiddle.net/2wm5uta3/

Js:

var str = "Every influenza season brings with it uncertainty about what strain will predominate and how severe it will be. While much of the world still is focusing on COVID-19, the potential for another serious influenza season can’t be ignored, and the strain on the health care system of 2 epidemics could be severe.1 As the SARS-CoV-2 virus continues to spread across the country, the 2020-2021 influenza season will be particularly challenging.1 Recent influenza seasons have been particularly serious: 2017-2018 was one of the deadliest in decades, with an estimated 61,000 deaths, and 2018-2019 was one of the longest flu seasons, lasting 21 weeks.2 The 2019-2020 influenza season was on a trajectory to be particularly severe, especially for children because of a high prevalence of influenza B cases.3 However, the season ended 5 to 6 weeks earlier than anticipated after control measures were put in place to prevent COVID-194; even with the shortened season, the Centers for Disease Control and Prevention (CDC) estimated 34,157 deaths, which was on par with the number of cases from 2018-2019.2,3  Although influenza viruses cannot be controlled directly, there are several measures that can be taken to help mitigate the severity of the influenza season. Continue Reading Prevention In March 2019, the World Health Organization (WHO) announce"
var newStr = str.replace(/(.{1,500})/g, '$1<br/><br/>')
document.write(newStr)

Upvotes: 2

Views: 52

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371019

Lookahead for a space or the end of the string after the end of the pattern:

var str = "Every influenza season brings with it uncertainty about what strain will predominate and how severe it will be. While much of the world still is focusing on COVID-19, the potential for another serious influenza season can’t be ignored, and the strain on the health care system of 2 epidemics could be severe.1 As the SARS-CoV-2 virus continues to spread across the country, the 2020-2021 influenza season will be particularly challenging.1 Recent influenza seasons have been particularly serious: 2017-2018 was one of the deadliest in decades, with an estimated 61,000 deaths, and 2018-2019 was one of the longest flu seasons, lasting 21 weeks.2 The 2019-2020 influenza season was on a trajectory to be particularly severe, especially for children because of a high prevalence of influenza B cases.3 However, the season ended 5 to 6 weeks earlier than anticipated after control measures were put in place to prevent COVID-194; even with the shortened season, the Centers for Disease Control and Prevention (CDC) estimated 34,157 deaths, which was on par with the number of cases from 2018-2019.2,3  Although influenza viruses cannot be controlled directly, there are several measures that can be taken to help mitigate the severity of the influenza season. Continue Reading Prevention In March 2019, the World Health Organization (WHO) announce"
var newStr = str.replace(/(.{1,500})(?=\s|$)/g, '$1<br/><br/>')
document.write(newStr)

Upvotes: 3

Related Questions