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.
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):
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.
Related
I'm using Selenium 3.17.0.
I want to type a text from a javascript, but it doesn't work!
I have tried this (this types the text "undefined")
and this (this types the entire script as text)
but nothing works! maybe I'm doing wrong the javascript but I don't know, I'm new in all of this, please help!
btw this is my first post, sorry if I'm doing it wrong.
If I understand your question correctly, you are trying to generate a random string using javascript and then store the value in a variable. Then enter that variable value using type.
you have to use execute script command to run the javascript and target (your javascript) should be return "free text" + Math.floor(Math.random()*100) as shown in the below screenshot.
I am running an API to retrieve email from external system. I managed to get HTML code from the returned JSON and store it in a variable. Now, I would like to run some further operations on this HTML - for example get all elements with
[data-type="whatever"].
It would be easy in html document:
var x = document.querySelectorAll('[data-type="whatever"]');
However the HTML document I want to work with is stored in the variable so the code I write in API does not recognise it as a document. How can I do it? Any suggestions with vanilla JS?
You can try something like this.
let rawDoc = '<html><head><title>Working with elements</title></head><body><div id="div1">The text above has been created dynamically.</div></body></html>'
let doc = document.createElement('html');
doc.innerHTML = rawDoc;
let div1 = doc.querySelector('#div1');
console.log(div1)
What if you use innerHTML? or maybe I don't fully understand the question.
Since you are working without a document you have 2 options.
1. Use regex to get what you need (something like /<.+>.+ data-type="whatever".+<\/.+>/gi) should do (but for an exact match you may need to make something better).
2. Insert the html in a hidden part of the dom and select what you need from it (like in Zohir answer - he provided a good example).
I used following code with angular to store whole html content in a variable and pass it as argument to call API.
var htmlBody = $('<div/>').append($('#htmlBody').clone()).html();
This might work for you as i was working on sending email to pass invoice template so try this.
I need to pass a variable to a javascript function,but I got a little trouble.
In the .cs file,I write like this:
string id = "someid";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction("+id+")\">"));
I need to use the value of this id,but in the console.log(id),it just shows "object",not the "someid",what's the problem?
Look at the generated HTML:
<input type="button" onClick="myFunction(someid)">
You are generating a variable name when you want a string literal.
Add some quote marks.
Whenever you have a problem that manifests in the browser: Look at the code the browser is dealing with first. You should always start by determining if the JS you want is not working or if the server side code is not generating the JS you want.
just add '' around id as i did below will resolve your issue
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
Use the single quotes around id:
'"+id+"' (notice the single quotes then double quotes)
The problem is that it is looking for a saved variable on the page named someid, which is why you are getting the object in the console. Pranay Rana has a good solution which is to make sure the string someid is surrounded by single quotes.
Well, question maybe doesn't correct, but I give a shoot.
My script:
OMG, testing
In real, without javascript url look like:
http://localhost/url=google.com
In here wanna add this: &format=txt
So, the correct url then would be:
http://localhost/url=google.com&format=txt
How to add &format to the javascript "script" which showed at the
top?
is this what you want:
OMG, testing
Just add it to the link using +, unless I misunderstood the question.
OMG, testing
Assumes txt is a variable, otherwise put it all in the quotes.
This will do it ,, you have to do same as you did for the localhost
OMG, testing
like that :
OMG, testing
You only have to add the text to the string of your url
I'm trying to set a cookie with a value that is made up of part of a referring url. The referring url looks something like:
http://webct.university.ac.uk/webct/urw/lc715920578061.tp721521425061/courseMenu.dowebct?
and I need to extract:
lc715920578061.tp721521425061
for reuse in another function.
Would this be regex? Could anyone help with the syntax?
Many thanks
Matthew
You can use replace with a regex and split like this:
var desired_part = document.referrer.replace(/^https?:\/\//gi, '').split('/')[3];