【问题标题】:Not all  s are being replaced converting text from a table cell to a textarea并非所有 s 都被替换将文本从表格单元格转换为文本区域
【发布时间】:2014-09-24 17:14:58
【问题描述】:

我再次需要你的帮助,

似乎并非所有的 ;nbsp 都被捕获并转换为适当的文本空间。为什么替换函数会捕获一些 ;nbsp 的而不是捕获和转换其他的。

我附上了问题的图片。

这是一个快速的小提琴:http://jsfiddle.net/n6vwrt2v/

我对此感到非常困惑。

有问题的代码:

var y = document.getElementById("table1").children[0].children[0].innerHTML

//some processing first to replace the <br>'s before the final output

y = y.replace(/\s*<br\s*\/?>\s*/g, '\n')

y = y.replace('&nbsp;',' ')

【问题讨论】:

  • y = y.replace(/&amp;nbsp;/,' ')

标签: javascript regex string text replace


【解决方案1】:

尝试遍历字符串并替换,直到没有剩余。替换只会替换字符串中的第一个。示例:

while(y.indexOf("&nbsp;") != -1)
{
   y.replace("&nbsp;", " ");
}

【讨论】:

  • 我试过了,但你的循环卡住了,什么也没发生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
相关资源
最近更新 更多