要添加的Javascript正则表达式<;br>;每n个单词后-无CSS
我正试图写一个正则表达式,但我似乎一事无成 我有一句话如下:要添加的Javascript正则表达式<;br>;每n个单词后-无CSS,javascript,regex,Javascript,Regex,我正试图写一个正则表达式,但我似乎一事无成 我有一句话如下: "Hey, diddle, diddle, The cat and the fiddle, The cow jumped over the moon; The little dog laughed To see such sport, And the dish ran away with the spoon." 我想在每个n字数中添加一个。例如,如果我每3个单词做一次,它会像这样 "Hey, diddle, diddle,<b
"Hey, diddle, diddle, The cat and the fiddle, The cow jumped over the moon; The little dog laughed To see such sport, And the dish ran away with the spoon."
我想在每个n
字数中添加一个
。例如,如果我每3个单词做一次,它会像这样
"Hey, diddle, diddle,<br/>
The cat and<br/>
the fiddle, The<br/>
etc..."
“嘿,欺骗,欺骗,
猫和猫
小提琴,小提琴
等等……”
我知道word wrap css,我纯粹是在寻找一个javascript解决方案。我不确定是否可以使用正则表达式。编写一个选择3个单词的正则表达式,然后用附加的
替换它
函数字包装(字符串,numWordsPerLine){
返回字符串.replace(
RegExp('(?:\\S+\\S*){'+numWordsPerLine+'}',g'),
“$&
”
);
}
var input=“嘿,骗人,骗人,猫和小提琴,牛跳过了月亮;小狗看到这样的运动哈哈大笑,盘子和勺子一起跑掉了。”;
document.body.innerHTML=wordWrap(输入,3)代码>为什么在S之前有额外的``字符?@AlexanderSolonik需要额外的反斜杠,因为我使用的是字符串文字而不是正则表达式文字,所以我必须避开反斜杠。