简体   繁体   English

为段落的每个换行符添加跨度

[英]Add span for each line break of a paragraph

I am having some trouble to add a span to each line of a paragraph.我在为段落的每一行添加跨度时遇到了一些麻烦。 I want to do that to animate my text like the SplitText plugin from GSAP.我想这样做来动画我的文本,就像 GSAP 的 SplitText 插件一样。

I have looked up for a way and this works fine for each letter of my paragraph :我一直在寻找一种方法,这对于我段落中的每个字母都很好用:

    var regex = /([^\x00-\x80]|\w)/g
    var textWrapper = document.querySelector(
      ".developpement-description .lines"
    );
    textWrapper.innerHTML = textWrapper.textContent.replace(
      regex,
      "<span class='letter'>$&</span>"
    );

So I thought that by changing the regex to match a linebreak it will work :所以我认为通过更改正则表达式以匹配换行符,它会起作用:

var regex = /[\n\r]\w/g;

But it doesn't...但它不...

Could someone help me ?有人可以帮助我吗?

尝试更改您的正则表达式,如下所示

var regex = /[\n\r]/gm;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM