用javascript关闭标记

用javascript关闭标记,javascript,html,text-formatting,Javascript,Html,Text Formatting,我试图用javascript关闭标记,但当它写入文档时,正斜杠总是丢失。我试着在前面加一个反斜杠(\/),但似乎确实有帮助。我一直在源代码页面上看到。代码如下: var temp_index = 0, line_count = 0; while (temp_index < text.length) { if ((text.charAt(temp_index) == "\n") && (line != line_count)) { if (li

我试图用javascript关闭标记,但当它写入文档时,正斜杠总是丢失。我试着在前面加一个反斜杠(\/),但似乎确实有帮助。我一直在源代码页面上看到
。代码如下:

var temp_index = 0,
    line_count = 0;
while (temp_index < text.length) {
    if ((text.charAt(temp_index) == "\n") && (line != line_count)) {
        if (line_count < line) line_count++;
        if (line_count == line) {
            text = text.substring(0, temp_index) + "<pre id='line'>" + text.substring(temp_index);
            temp_index++;
            while ((text.charAt(temp_index) != "\n") && (temp_index < text.length)) temp_index++;
            text = text.substring(0, temp_index - 1) + "<\pre>" + text.substring(temp_index);
        }
    }
    temp_index++;
}
return text;
Heres's the last line
<pre id='line'>Here's the current line</pre>
Here's the next line
Here's the final line
var temp_index=0,
行计数=0;
while(临时索引<文本长度){
if((text.charAt(temp_index)=“\n”)&&(line!=line_计数)){
if(line_count
我希望得到:

Here's the last line
<pre id='line'>Here's the current line
Here's the next line
Here's the final line</pre>
这是最后一行
这是当前线路
这是下一行
这是最后一行
但我得到了:

if (line_count == line) {
    text = text.substring(0, temp_index) + "<pre id=\"line\">" + text.substring(temp_index);
    temp_index++;
    while ((text.charAt(temp_index) != "\n") && (temp_index < text.length)) temp_index++;
    text = text.substring(0, temp_index - 1) + "</pre>" + text.substring(temp_index);
    break;
}
这是最后一行
这是当前线路
这是下一行
这是最后一行
我做了一个快速修复,将行末尾的\n替换为标记。 即使它解决了这个问题,它也会导致键盘输入出现错误。这是更新后的代码

temp_index = 0;
text = "this is \n a test \n of some info";

text =  text.substring( 0, temp_index )
     + "<pre id=\"line\">"
     + text.substring( temp_index );

temp_index++;

while( ( text.charAt( temp_index ) != "\n" ) && ( temp_index < text.length ) ) temp_index++;

text = text.substring( 0, temp_index - 1 )
     + "</pre>"
     + text.substring( temp_index - 1 );

alert(text);
if(行数==行){
text=text.substring(0,临时索引)+“”+text.substring(临时索引);
temp_index++;
而((text.charAt(temp_index)!=“\n”)和&(temp_index
此代码语法正确-初始化
临时索引
文本
,并省略循环外部的
中断

<pre id="line">this is</pre> 
 a test 
 of some info
\n”);

如果你知道当前行,想在它前面加上
并在它后面加上
是的,我刚加过,我仍然看到
你不需要转义正斜杠,所以“//”实际上等于“/”。反斜杠是另一回事,所以如果字符串中有“\\”,它将导致“\“。不确定示例是否关闭,但需要初始化
temp_index=0和你的
中断
不在你的
内,而
循环(你没有花括号)你的预期输出是什么?临时索引已初始化,我删除了中断。我更新了问题以显示预期输出我更新了代码以显示父while循环,并且我已经初始化了temp_index和line_count
text
变量是否包含当前行?它包含许多行,包括当前行。要突出显示的行由“line”指定。在这种情况下,我可能只需在
\n
上执行
split()
,替换当前行的数组值,然后执行
join()
。我刚刚尝试过,但它会在每行末尾添加逗号并删除所有\n
text = "this is \n a test \n of some info";
text = text.replace(/(.+?)\n/, "<pre id=\"line\">$1</pre>\n"); 
// update line 2
line = 2;

// sample text
text = "this is\n"+
       "a test\n"+
       "of some info";

// split to an array on newlines
vals = text.split("\n");

// replace the second line, which has an index of 1 (or line-1)
vals[line-1] = "<pre id=\"line\">" + vals[line-1] + "</pre>";

// join back to an array using newlines
text = vals.join("\n");
this is
<pre id="line">a test</pre>
of some info