[英]Help with replacing a text with regex
I have the following text in a file called build.xml:我在名为 build.xml 的文件中有以下文本:
component.rollup=true
component.rollup.modules.buildfiles=io-base.xml, io-form.xml, io-xdr.xml
component.rollup_dir=/base
I want it to add one file in it so it will become:我希望它在其中添加一个文件,因此它将变为:
component.rollup=true
component.rollup.modules.buildfiles=io-base.xml, io-form.xml, io-xdr.xml, io-extended.xml
component.rollup_dir=/base
How can I do that with regex?我怎么能用正则表达式做到这一点?
I'm using javascript.我正在使用 javascript。
Thanks!谢谢!
EDIT:编辑:
Forgot to tell you that the files are not static, they may change.忘了告诉你文件不是 static,它们可能会改变。 Only
仅有的
component.rollup.modules.buildfiles=
will always be there.将永远在那里。
Assuming you have already read the contents of the file:假设您已经阅读了文件的内容:
fileContents.replace(
/^(component\.rollup\.modules\.buildfiles\s*=.*)$/m,
'$1, io-extended.xml');
Try this (assuming the variable with the XML contents in it is called myXmlContent
):试试这个(假设其中包含 XML 内容的变量称为
myXmlContent
):
if (!myXmlContent.match(/^component\.rollup\.modules\.buildfiles=.*io-extended\.xml/im) {
myXmlContent = myXmlContent.replace(/^(component\.rollup\.modules\.buildfiles=.*)$/im, '$1, io-extended.xml');
This will ensure it isn't added twice.这将确保它不会被添加两次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.