Javascript 是否可以插入<;br>;仅在超过5个单词的字符串上每5个单词后添加一个标记,并添加<;br>;加上“,”&引用;(dot)

Javascript 是否可以插入<;br>;仅在超过5个单词的字符串上每5个单词后添加一个标记,并添加<;br>;加上“,”&引用;(dot),javascript,Javascript,我想在字符串中插入s。规则: 在每个句点后插入一个 每5个单词后插入一个 例如: This is a string. For demo to split the text. If its long more then 5 characters. But some string is short. And it will not split. the short length string it will only split the long string which is more the

我想在字符串中插入

s。规则:

  • 在每个句点后插入一个

  • 每5个单词后插入一个

例如:

This is a string. For demo to split the text. If its long more then 5 characters. But some string is short. And it will not split. the short length string it will only split the long string which is more then 5 words.";
期望输出:

This is a string.<br>For demo to split the<br>text.<br> If its long more then<br>5 characters.<br>But some string is short.<br>And it will not split.<br>The short length string it<br>will only split the long<br>string which is more then<br>5 words.
这是一个字符串。
用于演示拆分
文本。
如果其长度超过
5个字符。
但某些字符串较短。
且不会拆分。
短字符串
只拆分长度超过
5个单词的长字符串。

我如何才能做到这一点?

使用正则表达式匹配5个非空格序列(其中非空格不包括
),或匹配
。然后在匹配后插入一个

const str=“这是一个字符串。对于演示,可以拆分文本。如果它的长度超过5个字符。但是有些字符串很短。它不会拆分。短长度字符串只会拆分超过5个单词的长字符串。”;
常量输出=str.replace(
/(?:[^\s.]+\s+{5}\./g,
“$&
” );
控制台日志(输出)我已经更新了问题,请再次帮助我。因为这是最后一行,所以我不需要

。哦,我忘了,我现在就更新它。这是一个错误,请使用负数字符集来匹配非
s的非空格-或者,匹配
,并进行相同的替换。请给出一个示例?请问到目前为止你试过什么?