简体   繁体   English

将长单词拆分为块的正则表达式堆栈表达式

[英]Regex stack expressions to split long words into chunks

I tried to stack two regex expressions, but without any luck.我试图堆叠两个正则表达式,但没有任何运气。

In my case I need to find all words with length 50 and larger在我的例子中,我需要找到所有长度为 50 和更大的单词

\w{50,}

and now I need to split only those matches in chunks by 10 characters.现在我只需要将那些匹配项分成 10 个字符的块。

.{1,10}

I was unable to apply second expression to the first expression matches.我无法将第二个表达式应用于第一个表达式匹配项。 Do you've any ideas?你有什么想法吗?

Thanks!!!谢谢!!!

Javascript replace() can take a function as replacement. Javascript replace()可以将 function 作为替换。

str = str.replace(/\w{50,}/, function(m) {
  return m.match(/.{1,10}/g).join(' ');
});

See this demo at tio.run - or a bit shorter one: 在 tio.run 上查看此演示- 或者更短的演示:

str = str.replace(/\w{50,}/, m => m.match(/.{1,10}/g).join(' '));

No idea if it's possible in JS with just one regex.不知道在 JS 中是否可以仅使用一个正则表达式。 In PCRE: (?:\G\B|\b(?=\w{50}))\w{10}\B\K在 PCRE 中: (?:\G\B|\b(?=\w{50}))\w{10}\B\K

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

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