JavaScript替换/ r与<br/>无法正常工作

时间:2016-12-13 09:45:56

标签: javascript replace

现在,在某人将此标记为重复问题之前,我已查看其他帖子中存在此问题并且他们尚未解决我的问题。

所以我有一个字符串,其中有一些\r需要转换为<br />

但没有任何工作

output = output.replace("\\r", "<br />"); 

是我最接近的工作是当我的字符串有/ r / r时它会用第一个r替换

我在字面上尝试的其他所有事情都没有做任何事情,包括

replace("\r", "<br />");
replace(/\\r/g, "<br />")
replace(/\r/g, "<br />")
replace(/\n/g, "<br />")

和其他一些我不记得了。 我在堆栈上的其他帖子上发现的所有那些都没有用。他们甚至没有取代\r

以下是其中一名9年级学生在以下地点于上午8:30在他们的核心科目小组中见面的一个例子\ r \ r9A - 外面的ESOL BLOCK \ r9B - 科技电脑室\ r9C - ESOL ROOM。 r \ rPLEES穿TRACKSUIT。\ r \ n它在一个循环中有一堆不同的字符串 -

因为人们咩咩它应该在这里工作是一个更代表的代码视图

<script>

            TheOutput.innerText = "testbox";



            function nl2br (str) 
                {
                    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br />' + '$2');
                }


            /* set up drag-and-drop event */
            function allowDrop(e) 
                {
                    e.preventDefault();
                }

            function handleDrop(e) 
                {
                    e.stopPropagation();
                    e.preventDefault();

                    var files = e.dataTransfer.files;
                    var i,f;
                    for (i = 0, f = files[i]; i != files.length; ++i) 
                        {
                            var reader = new FileReader();
                            var name = f.name;

                            console.log("==== current file in file reader ====");
                            console.log(name);

                            reader.onload = function(e) 
                                {
                                    var data = e.target.result;

                                    /* if binary string, read with type 'binary' */
                                    var workbook = XLSX.read(data, {type: 'binary'});

                                    //----------------------------------
                                    /* DO SOMETHING WITH workbook HERE */
                                    //----------------------------------

                                    /*
                                    var first_sheet_name = workbook.SheetNames[0];
                                    var address_of_cell = 'B3';

                                    /* Get worksheet 
                                    var worksheet = workbook.Sheets[first_sheet_name];

                                    /* Find desired cell 
                                    var desired_cell = worksheet[address_of_cell];

                                    /* Get the value 
                                    var desired_value = desired_cell.v; */





                                    var sheet_name_list = workbook.SheetNames;
                                    sheet_name_list.forEach(function(y) 
                                        { /* iterate through sheets */
                                            var worksheet = workbook.Sheets[y];




                                            var cellCount = 0;
                                            var currentCell;
                                            var output;

                                            for (z in worksheet) 
                                                {
                                                    //z is the address of the cell
                                                    //y is the name of the worksheet

                                                    /* all keys that do not begin with "!" correspond to cell addresses */
                                                    if(z[0] === '!') continue;

                                                    console.log("==== JS Object ====");
                                                    console.log(worksheet[z].v);

                                                    console.log("==== JSON Object ====");
                                                    console.log(JSON.stringify(worksheet[z].v));

                                                    console.log("==== Other Stuff ====");
                                                    console.log(y + "!" + z + "=" + JSON.stringify(worksheet[z].v));


                                                    output = (JSON.stringify(worksheet[z].h)).toString();
                                                    output = output.replace(/"/g,"");
                                                    //(/\r/g, '<br/>')
                                                    //output = output.replace("\r", "<br/>");

                                                    output = output.replace(/\r/g, "<br />")

                                                    console.log("Grab output ans strip strings: " +output);

                                                    //output = nl2br (output);
                                                    //console.log("output after nl2br function: " +output);

                                                    console.log("======================");

                                                    //output = worksheet[z].w;

                                                    var div = document.createElement('div');
                                                    document.body.appendChild(div);
                                                    div.className = 'DivCells';


                                                    div.id = 'test'+ cellCount;


                                                    currentCell = div = document.getElementById('test'+ cellCount);
                                                    currentCell.innerHTML = output;

                                                    currentCell.style.left = '32px'; 
                                                    currentCell.style.top = ((cellCount+1)*10)+'px';


                                                    //TheOutput.innerText = output;

                                                    cellCount = cellCount + 1;


                                                }
                                                cellCount = 0;
                                        });

                                    //display the value
                                    //TheOutput.innerText = desired_value.toString();


                                    /*------------------------------------------
                                                END OF WORKBOOK STUFF
                                    --------------------------------------------*/

                                };

                            reader.readAsBinaryString(f);
                        }
                }
            //drop_dom_element.addEventListener('drop', handleDrop, false);

            //var workbook = XLSX.readFile('Kmaranotices.xlsx');




        </script>

1 个答案:

答案 0 :(得分:0)

好的,我设法通过创建一个函数来修复它

function nl2br (str) 
                {
                    //returns an string that replaces all the \r with new lines
                    return (str + '').replace(/\\r/g,'<br />');
                }

然后用它来处理我的输出

output = (JSON.stringify(worksheet[z].h)).toString();
output = nl2br (output);
output = output.replace(/"/g,"");

这是一个非常奇怪的问题,我很高兴终于解决了它。