1

I have the following text in a file called 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.

Thanks!

EDIT:

Forgot to tell you that the files are not static, they may change. Only

component.rollup.modules.buildfiles=

will always be there.

2
  • You're editing files with javascript? Commented Jun 10, 2011 at 4:54
  • @Michael, IE allows this through ActiveX, or it could be server-side JS, e.g. node.js or Rhino. Commented Jun 10, 2011 at 4:56

2 Answers 2

2

Assuming you have already read the contents of the file:

fileContents.replace(
  /^(component\.rollup\.modules\.buildfiles\s*=.*)$/m,
  '$1, io-extended.xml');
Sign up to request clarification or add additional context in comments.

4 Comments

The only potential problem with this is that if it is run more than once it will keep adding the io-extended.xml file to the list. Hence why I made sure in my answer that the line ended in io-xdr.xml.
+1 good answer, but you should include the = after buildfiles to be safe.
@GregL: true, I suppose the regex could do a negative lookahead assertion for the "io-extended.xml" filename. Note that from OP's update I don't think it's safe to assume that any filenames are present though.
Or you could just do a simple .match() check first as I did in my answer. Easier to understand, I find, if a little less technically impressive.
1

Try this (assuming the variable with the XML contents in it is called 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.