简体   繁体   English

使用多行字符串替换Javascript多行字符串

[英]Javascript multiline string replacement with multiline string

I was trying to replace multiline string with another multiline string using javascript. 我试图使用javascript将多行字符串替换为另一个多行字符串。 How do I do that? 我怎么做?

For example the pseudo code for this could be as shown below, where the string to search and set are runtime variables/arguments (not hardcoded as shown below), so we cannot create fixed regular expressions. 例如,伪代码可以如下所示,其中要搜索和设置的字符串是运行时变量/参数(不是硬编码,如下所示),因此我们无法创建固定的正则表达式。 Any idea? 任何想法?

The string to search for looks like, 要搜索的字符串看起来像,

<tr>

    <td colspan="3" class="footer">&nbsp;</td>

</tr></tbody></table>

and let the string to replace be, 并让字符串替换为,

<tr>

   <td colspan="3" class="footer">New Arrivals!!!</td>

</tr>

<tr>

<td colspan="3" class="footer">&nbsp;</td>

</tr></tbody></table>

Pseudo code, 伪代码,

var regExp = new RegExp(strMultiLineTextToSearch.trim(), "gm");
var matches = htmlContent.match(regExp);
alert(matches.length);
if(matches.length > 0){
    var newHtml= htmlContent.replace(regExp , strMultiLineTextToSet );
}

Using Firefox 22.0 使用Firefox 22.0

also, the strings are prased from webpage using some string foo and innerHTML. 另外,字符串是使用一些字符串foo和innerHTML在网页上引用的。

EEEK! EEEK! Don't use HTML manipulation to add content to a table. 不要使用HTML操作将内容添加到表中。 That would be like adding a small detail to a big painting by having a painter describe the picture to you, then paint a new picture based on that description, except you change that little detail in the description. 这就像通过让画家向你描述图片来为一幅大画添加一个小细节,然后根据描述绘制一张新图片,除了你在描述中改变那些小细节。

Not just it's incredibly slow, but the painter is likely to not use the very same words you used when telling him to draw the picture the first time. 不仅仅是它非常缓慢,但画家很可能不会使用你第一次告诉他画画时使用的相同词语。 You will be confused if he says "young woman" instead of "lady". 如果他说“年轻女人”而不是“女士”,你会感到困惑。 You wanted the lady to hold something, but there's only a young woman. 你想要那位女士拿东西,但只有一位年轻女士。 Instead, you should tell the painter to give the lady that something. 相反,你应该告诉画家给女士一些东西。

The same applies here: are you sure you use the exact same amount of whitespace in the string you're searching as is in the table? 这同样适用于:你确定你在表中搜索的字符串中使用了完全相同数量的空格吗? If there isn't, the string won't be found. 如果没有,则找不到该字符串。 Even if there is, how can you be sure the browser doesn't rearrange the attributes within a tag (IE loves to sort them alphabetically)? 即使有,你怎么能确定浏览器不重新排列标签内的属性(IE喜欢按字母顺序排序)? Add whitespace after the arguments? 在参数后添加空格? Some browser extension may add its own attributes or classes... 某些浏览器扩展可能会添加自己的属性或类...

Instead, use DOM manipulation. 相反,使用DOM操作。 That's the picture. 那就是图片。 HTML is its description. HTML就是它的描述。

If you add an ID to that row, it will be easy to find from javascript. 如果您向该行添加ID,则可以通过javascript轻松找到。 Then create the new element and place it before that row. 然后创建新元素并将其放在该行之前。

var lastRow = document.getElementById("last-row")
var td = document.createElement("td")
td.className = "footer"
td.colspan = 3
var text = document.createTextNode("New arrivals!!!")
td.appendChild(text)
lastRow.parent.insertBefore(lastRow, td)

If the table is easy to find, you can access its last row via that table: 如果表很容易找到,您可以通过该表访问其最后一行:

var table = document.getElementById("my-table")
var lastRow = table.rows[table.rows.length - 1]
...

It seems you are using the table to layout your page. 您似乎正在使用该表来布局页面。 This is not a clean approach (the page is not semantically a table, lots of unneccessary elements; please note the distinction between <table> and display:table ). 这不是一个干净的方法(页面在语义上不是一个表,许多不必要的元素;请注意<table>display:table之间的区别)。 Adding an empty spacing cell is almost certainly wrong. 添加空间距单元几乎肯定是错误的。 Use CSS margin-bottom or padding-bottom instead (unless you're making an HTML email - in which case, use CSS anyways, then convert to a mail-safe form using some freeware web service that exists for that purpose). 使用CSS margin-bottompadding-bottom (除非您正在制作HTML电子邮件 - 在这种情况下,无论如何都要使用CSS,然后使用为此目的而存在的一些免费软件Web服务转换为邮件安全表单)。 CSS is not hard. CSS并不难。 Not using it is much harder. 不使用它要困难得多。

The problem I see here is that you're using double quotes where they are not allowed and lack of \\n in the end of each line in your strings: 我在这里看到的问题是你在不允许使用双引号,并且在字符串的每一行末尾缺少\\n

var strMultiLineTextToSearch =
"<tr>

    <td colspan="3" class="footer">&nbsp;</td>

</tr></tbody></table>";

should be 应该

var strMultiLineTextToSearch =
"<tr>

    <td colspan='3' class='footer'>&nbsp;</td>

</tr></tbody></table>";

You can't have Strings spanning across multiple lines. 你不能让字符串跨越多行。 If you want a String that contains newline characters you'll have to use escape sequences: \\n. 如果你想要一个包含换行符的字符串,你必须使用转义序列:\\ n。 Also pay attention not to use double quotes in a string that is itself enclosed in double quotes - you need to escape those, too. 另外注意不要在用双引号括起来的字符串中使用双引号 - 你也需要逃避它们。

var foo = "I am a string that contains a \\" double quote"

or 要么

var foo = 'I am a string that contains a " double quote'

are legal while 是合法的

var foo = "I am a string that contains a " double quote"

is not 不是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM