javascript - 帮助用正则表达式替换文本

标签 javascript regex

我在名为 build.xml 的文件中有以下文本:

component.rollup=true
component.rollup.modules.buildfiles=io-base.xml, io-form.xml, io-xdr.xml
component.rollup_dir=/base

我希望它在其中添加一个文件,这样它将变成:

component.rollup=true
component.rollup.modules.buildfiles=io-base.xml, io-form.xml, io-xdr.xml, io-extended.xml
component.rollup_dir=/base

我如何使用正则表达式做到这一点?

我正在使用 javascript。

谢谢!

编辑:

忘了告诉你文件不是静态的,它们可能会改变。只有

component.rollup.modules.buildfiles=

永远在那里。

最佳答案

假设您已经阅读了文件的内容:

fileContents.replace(
  /^(component\.rollup\.modules\.buildfiles\s*=.*)$/m,
  '$1, io-extended.xml');

关于javascript - 帮助用正则表达式替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302093/

相关文章:

javascript - 如何将 JSON 保存到本地文本文件

javascript - 使用延迟和传递变量链接多个 AJAX 调用

javascript - onClick 不调用函数

javascript - Typescript - 替换 url 映射中的字符串

javascript 检查字符串末尾的特殊字符

regex - 相同的正则表达式用 sed 和用 grep 的评估不同

regex - 我如何在 Go 中使用/访问捕获组?

javascript - 如何抑制 SonarQube JavaScript 问题

javascript - 使用 CSV 文件预处理 Highchart 数据

Java 字符串解析,什么更快?正则表达式或字符串方法?