gpt4 book ai didi

javascript - 在 jquery 中翻转多行文本的最快方法

转载 作者:行者123 更新时间:2023-11-30 12:24:44 25 4
gpt4 key购买 nike

我正在寻找最短的未混淆代码行来反转 jquery 中的多行字符串。例如,

abcd

edfg

hijk

应该变成

dcba

gfde

kjih

一个极其复杂的例子如下:

function fliptext() {
var text = document.getElementById('input_output').value;
text = text.replace(/\r/gi, '');
text = text.replace(/([^a-z 0-9\n])/gi, ' $1 ');
text = text.split('\n').reverse().join('\n');
text = text.split('').reverse().join('');
text = text.replace(/ ([^a-z 0-9\n]) /gi, '$1');
document.getElementById('input_output').value = text;
}

什么是更好的方法?

最佳答案

你可以做到;

text = text.split('\n').map(function(el) {return el.split('').reverse().join('')}).join('\n');

首先,我们将文本按换行符分割并得到一个行数组,然后反转每一行,然后用换行符('\n')连接数组并得到一个字符串。

关于javascript - 在 jquery 中翻转多行文本的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29819672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com