引用号:JS fiddle
我有一个<pre>
内部包含多行的元素。使用 JQuery,我正在调用 .html()
获取innerHTML
,然后执行字符串 replace()
在返回的 HTML 字符串上。但是,替换仅适用于第一行,其余行可能会失败,因为返回的 innerHTML
有换行符。
您能否建议一种将两行都放在 <pre>
内的解决方法?更换了?
最佳答案
请注意有关在 replace
函数中使用 flags
参数的警告,该警告位于 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace :
Note: The flags argument is a non-standard Mozilla extension. A string specifying a combination of regular expression flags. The use of the flags parameter in the String.replace method is non-standard. Instead of using this parameter, use a RegExp object with the corresponding flags.
(强调的是我的)
因此,您应该使用正确的正则表达式,而不是使用这些参数:
string = string.replace(/<your-login>/g, userName);
string = string.replace(/<repository-name>/g, repoName);
关于JavaScript:替换包含换行符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345242/