[英]Trim each 3 words in newline in string (jquery)
我想在每个换行符上修剪 3 个字母。 但是当我拆分它只拆分第一行时,另一个换行符已经消失了。 请帮忙。 谢谢你。
这是代码:
var mytext = '(1x) test0, (2x) test1, (3x) test2';
var formattedString = mytext.replace(/,/g, '\n');
console.log("FORMATTED STRING", formattedString)
const output = formattedString
.split('', 4)
.reduce((o, c) => (o.length === 4 ? `${o}${c}` : `${o}${c}`), '')
console.log("SPLIT", output)
可以在这里访问代码:
https://jsfiddle.net/mqnkx2uc/14/
我想要这样的 output:“(1x) \n (2x) \n (3x)”
我将使用正则表达式首先匹配 a )
,然后匹配非(
字符,并替换为)\n
:
var mytext = '(1x) test0, (2x) test1, (3x) test2'; console.log( mytext.replace( /\)[^\(]+/g, ')\n' ).trim() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.