javascript - 每 x 个字符强制换行,但保留单词不被剪切

标签 javascript string replace

我正在返回一长串单词,需要添加一些换行符以使其可读。

使用下面的方法我可以添加中断,但它会在不同的地方 chop 单词。

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)

最佳答案

在模式结尾后先行查找空格或字符串结尾:

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)

关于javascript - 每 x 个字符强制换行,但保留单词不被剪切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63531818/

相关文章:

c - 嵌套strtok_r() : succesive tokens contain parent delimiter

regex - Notepad++ 正则表达式用不同的替换替换多个值

javascript - Meteor 找不到重新导出的模块

javascript - vuex 状态和 getter 已更新,但计算值没有反应

javascript - 等待 lodash 找到完成

string - Julia 中的单引号和双引号

c++ - 从自定义类到 std::string 的转换

Javascript 替换未转义的斜杠

java - 一次替换多个子字符串

javascript - 从 Node.js 加密库获得奇怪的结果