我们的报告页面附加了 <pre>
页面底部逐行列出信息。
示例:
<pre>
Site Report Info
This is where any error will appear as a query string of numbers: 938109283091238109281092
This is where the account ID will be.
This is where the account reference pin will be.
So on...
So on...
So on...
So on...
So on...
</pre>
使用 javascript 或 jquery,也许是正则表达式,如何将所有这些放入一个数组中,其中每一行都是一个数组元素?我假设使用正则表达式,因为确定行的方法是通过识别换行符 \n
?
最佳答案
varlines = $('pre').text().split('\n')
应该可以解决问题。
当然,您不需要使用 jQuery 来获取文本,但如果您正在进行 Web 编程,则 jQuery 非常普遍。
您可能还想 trim 结果以消除多余的空格(或不消除,具体取决于您的应用程序):
var lines = $('pre').text().split('\n').map(function(l) { return l.trim(); });
关于javascript - 使用 Javascript 将 <pre> 中的内容放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26412281/