javascript替代sed脚本 [英] javascript alternative to sed script

查看:71
本文介绍了javascript替代sed脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sed脚本,它将读入一个文件,如果一行以空格开头,则删除前面的换行符和匹配的空格,以便该行和前一行连接在一起。

I have a sed script which will read in a file and, if a line begins with a space, remove the preceding line break and the matched whitespace so that that line and the preceding line are joined together.

sed -e :a -e '$!N;s/\n / /;ta' -e 'P;D' input_file_name.txt

有没有办法让javascript使用正则表达式做同样的事情?

Is there any way to have javascript do the same using regular expressions?

我正在使用HTML5 File API

I am reading in the file using the HTML5 File API.

推荐答案

最简单的方法是读取整个文件然后进行替换,如:

Easiest way is to read the whole file and then do a replace like:

content = content.replace(/\r?\n /g, ' ');

如果文件适合内存,那么。

If the file fits in memory that is.

这篇关于javascript替代sed脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆