在javascript中,如何在N个字符计数后插入新行?
我有一个字符串,其中可能有新行“\n”字符。现在,我想在该字符串中每4(或n)个字符后插入新行“\n” 例如: (一) 输入:“我是约翰·多伊。” 输出:“我是\nJohn\nDoe” 在上面的示例中,在包含空格的4个字符后插入“\n” (二) 输入:“I\n John Doe” 输出:“I\nam J\nohn\nDoe” 在上面的示例中,在字符串中已存在第一个“\n”之后的4个字符后插入空格 (三) 输入:12345\n67890 输出:1234\n5\n6789\n0 (四) 输入:“1234\n56\n78901” 输出:“1234\n56\n7890\n1”在javascript中,如何在N个字符计数后插入新行?,javascript,jquery,algorithm,Javascript,Jquery,Algorithm,我有一个字符串,其中可能有新行“\n”字符。现在,我想在该字符串中每4(或n)个字符后插入新行“\n” 例如: (一) 输入:“我是约翰·多伊。” 输出:“我是\nJohn\nDoe” 在上面的示例中,在包含空格的4个字符后插入“\n” (二) 输入:“I\n John Doe” 输出:“I\nam J\nohn\nDoe” 在上面的示例中,在字符串中已存在第一个“\n”之后的4个字符后插入空格 (三) 输入:12345\n67890 输出:1234\n5\n6789\n0 (四) 输入:“12
到目前为止,我已经创建了一个函数,它在每4个字符之后插入“\n”,但是如果它已经存在于原始字符串中,它就不考虑“\n”。
function addNewlines(str) {
if (str.length >= 4) {
var result = '';
while (str.length > 0) {
result += str.substring(0, 4) + '\n';
str = str.substring(4);
}
return result;
}
return str;
}
我在每次按键时调用这个函数,传递原始字符串,获得输出,并进一步使用它。我希望你明白我在这里的意思。它应该保留以前插入的新行
让我知道我可以进一步解释。有更多的例子
function ngram_insert (n, ins, input)
{
var output = "";
var i = 0;
while (i < strlen(input))
{
if (i > 0 && i % n == 0)
{
output += ins;
}
output += input[i];
i++;
}
return output;
}
以下是我对所要求的最好的猜测:
function addNewLines (str) {
return str.replace (/(?!$|\n)([^\n]{4}(?!\n))/g, '$1\n');
}
一些测试字符串及其结果:
"I am John Doe.", -> "I am\n Joh\nn Do\ne."
"I\nam John Doe", -> "I\nam J\nohn \nDoe"
"12345\n67890", -> "1234\n5\n6789\n0"
"1234\n56\n78901", -> "1234\n56\n7890\n1"
"ABCD\nEFGH\nIJKL", -> "ABCD\nEFGH\nIJKL\n"
"1234", -> "1234\n"
"12341234" -> "1234\n1234\n"
对于那些对正则表达式感到神秘的人,这里有一个分类:
---------------------------- (?! Check that following character(s) are not
| ------------------------- $|\n Beginning of string or a newline character
| | --------------------- )
| | | -------------------- ( Start of capture group 1
| | || ------------------ [^\n] Any single character other than newline
| | || | -------------- {4} Previous element repeated exactly 4 times
| | || | | ----------- (?! Check that following character(s) are not
| | || | | | --------- \n a newline
| | || | | | | ------- )
| | || | | | | |------ ) End of capture group 1
| | || | | | | || ---- /g in replace causes all matches to be processed
| | || | | | | || |
/(?!$|\n)([^\n]{4}(?!\n))/g
函数f(n,ins,str){
如果(str.length==0)
返回“”;
变量i=str.indexOf(“\n”),len=str.length,newStr;
如果(i==-1){
i=1;
newStr=str[0];
}
否则{
newStr=str.substring(0,i+1);
}
var k=1;
而(k+i调用
f(4),“\n”,“I\namjohndoe”)
返回“I\nam J\nohn\nDoe”
请确认第二个示例,第一个插入的\n在5个字符之后,为什么?更多的例子也会有所帮助。我还发现\n
字符周围的*令人困惑,请使输入和输出完全符合您需要的字符串。我已删除了*。请不要考虑引号只是表示字符串。您的描述说,您希望在每四个字符(或现有新行之后的四个字符)之后添加换行符,但是您的示例只显示了添加到字符串中的单个换行符。到底是哪一个,总共只添加了一个换行符,还是每四个字符后添加一个换行符?谢谢,但是您的第二个示例输出仍然令人困惑,是否应该在Doe
之前添加一个\n
?您还声明“在4个字符后插入空格”:在什么条件下?您的第一个示例不应该还有一个\n
?这个字符串应该变成什么:“ABCD\nEFGH\nIJKL”-它应该仅仅保留现有的换行符,还是添加额外的换行符?我们猜测的原因是您还没有编写完整的规范。您需要解释您在每个edge案例中想要什么,并提供输入和输出的综合列表。谢谢。它返回预期的结果。
---------------------------- (?! Check that following character(s) are not
| ------------------------- $|\n Beginning of string or a newline character
| | --------------------- )
| | | -------------------- ( Start of capture group 1
| | || ------------------ [^\n] Any single character other than newline
| | || | -------------- {4} Previous element repeated exactly 4 times
| | || | | ----------- (?! Check that following character(s) are not
| | || | | | --------- \n a newline
| | || | | | | ------- )
| | || | | | | |------ ) End of capture group 1
| | || | | | | || ---- /g in replace causes all matches to be processed
| | || | | | | || |
/(?!$|\n)([^\n]{4}(?!\n))/g
function f(n, ins, str) {
if (str.length == 0)
return "";
var i = str.indexOf("\n"), len = str.length, newStr;
if (i == -1) {
i = 1;
newStr = str[0];
}
else {
newStr = str.substring(0, i + 1);
}
var k = 1;
while (k + i < len) {
newStr += str[k + i];
if (k % n == 0) {
newStr += ins;
}
k++;
}
return newStr;
}
function parseInput(str, char, length){
var split = str.split(char),
regex = RegExp('(.{' + length + '})','g');
split[split.length-1] = split[split.length - 1].replace(regex, '$1' + char);
return split.join(char);
}
console.log(parseInput("I am John Doe.", "\n", 4));
// output = "I am\n Joh\nn Do\ne."