I am running a regular expression against the DOM to return back an account status from a page.
This is the string on the page:
<h3>Status</h3><p>Completed</p>
And this is the Expression I'm currently using
<h3>Status</h3>[\s\S]*?<p>([\s\S]*?)</p>
My goal is to only get the Status of "Completed" from this string but not sure on how to do this. I have read a little on submatching; just not sure how to implement it.
re.match() returns an array containing the sub-matches for each capture group. So use:
var re = new RegExp('<h3>Status</h3>[\s\S]*?<p>([\s\S]*?)</p>');
var match = re.match(str);
var submatch = match[1];
This will work: /<h3>Status<\/h3>[\s\S]*<[^>]*>([^<]+)<.*/
See it working here: http://jsfiddle.net/M7kJ7/
But seriously... use DOM functions for that! Why a regex?
EDIT: Example of how you could solve it using DOM functions: http://jsfiddle.net/DycGh/
EDIT2: OK, after reading all the comments, I came to the conclusion that you do have valid reason to not access directly the database (you can't! they don't give you access to it)
And you can't use native DOM functions (you are not executing js directly on each page, but instead one central page is going to be used for searching the other pages)
,
However, I still don't think browser-side javascript is the correct path.
Using either server-side javascript (node.js), or some other language, like perl would be better. And using DOM, by means of a parser, is correct too.
If you choose with the node.js path, you can use node-htmlparser. From your node app you'll open each url, get the data using the parser's functions and then construct a json output. Your page will make an ajax request to node, and get its json results, which you will use to create the output.
If you go for perl, you can use HTML::DOM. The rest of the procedure would be similar.
It doesn't has to be perl or node.js, is just the options I know. With php, python or ruby you can do it too. (but you'll have to google for parsers)
But is best if you do it with a server-side script.
Related
First, I know that JSON doesn't support comments. I'm using them anyway because I need to write little notes to make it easy for people who aren't tech-savvy to edit this file.
I'm using double-slash comments, but I can switch to slash-star if needed.
"campaignID": "230109-stock-shop", // this is the method I want
I know the technique of creating extra comment fields like this, but it doesn't fit my needs.
"__comment": "this ISN'T the method I want"
I'm working entirely in client-side Javascript/jQuery. When I import a file with comments, it - of course - doesn't work.
$.getJSON('file.json', function (json) {//do things})
Is it possible to strip out the comments when importing the JSON file somehow? I tried to find a JS library or something that would do that, but everything I found is server-side, and I need everything to run client-side.
I'm also not super great with Javascript, so it's possible I'm missing something obvious.
I really appreciate any help you can give!
You can remove all the comments before trying to parse it.
You won't be able to use $.getJSON(), since that tries to parse the JSON itself. Use $.get(), remove the comment and parse it.
$.get('file.json', function(commented_json) {
json = JSON.parse(commented_json.replace(/\/\/.*/g, ''));
// do things
}, "text");
Note that this won't work properly if any of the strings in the JSON contain //. That would require using a real JavaScript parser so it can distinguish the context.
I wrote python code that looks like this and returns the enire JSON database.
from firebase import firebase
firebase = firebase.FirebaseApplication('https://name_name_name.firebaseio.com/')
result = firebase.get('/results', None)
I have tried to do the same thing in javascript with no success. This should be straight forward but I haven't seen any solutions.
The idea is to host this on the server. It will pass the result variable to an XML converter. The XML will print to either that same document or another document in the directory.
What is the JavaScript equivalent of my Python code?
Without any real information on where your code runs, you probably want https://www.npmjs.com/package/firebase, which can be used both in the client (e.g. browsers) as well as in standard OS context (offline/server/general scripting language).
for anything JavaScript, http://npmjs.com is your one stop "is there something to do X" open source shop.
I'm trying to find a way to programatically get the last value returned by the Javascript interpreter. Ruby's interpreter, to name an example, has the "_":
1 + 2 #=> 3
_ #=> 3
I would like to know if the same thing exists in Javascript.
EDIT:
Another way to maybe achieve this. Is there any syntax that supports the continuation of an expression in a newline? Something like this:
var a = \&
1 + 2;
a #=> 3
Some sort of combination of characters that tell the interpreter the expression continues in a newline (like the + for string concatenation).
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure using a script tag and successfully assign it from outside of its scope, something like this:
<script> var json_struct = </script>
<script src="http://domain.com/myjsonfile.json" type='application/json' ></script>
which, by the way, doesn't work. Surprisingly :)
PURPOSE:
Purpose of this research is to find if I can load a JSON data structure
using a script tag and successfully assign it from outside of its scope
There is no construct in browser based javascript that can do this.
The reason is that browsers, since the earliest Netscape days, have always initiated the script compiler upon the closing of the script tag. Regardless if it's javascript, VBscript (IE only) or Tcl (with the appropriate plugin).
Which means that any statement that is incomplete will simply be treated as a syntax error. Each <script> tag is basically treated as a single file.
What you're trying to do is similar to this in Ruby:
a = require 'one_plus_two.rb'
which I don't think works in Ruby.
However, in non-browser environments that support modules like Node.js, the method that imports module does in fact return a value (usually an object). So you can do something like this in node.js:
var a = require('my_data_file.js');
Unfortunately, the require function only works on local files. But Node.js is open source so you can always fork it and modify require to be able to source from http:// like PHP.
Alas, if what you're trying to do is browser scripting then the above point is moot.
So, I'm running these Selenium IDE tests against a site I'm working on. Everything about the tests themselves is running fine, except I would like to do a bit of clean-up once I'm done. In my MVC3 Razor based site, I have a JavaScript file with a function that gets a JsonResult from a Controller of mine. That Controller handles the database clean-up that Selenium IDE otherwise couldn't handle.
However, I'm having a hard time finding any sort of documentation on how to do this. I know I can do JavaScript{ myJavascriptGoesHere } as one of the Values for a line in the test, but I can't seem to find a way to tell it to go find my clean-up function.
Is it even possible for Selenium IDE to do this sort of thing?
If it comes down to it, I can just make a separate View to handle the clean-up, but I'd really like to avoid that if possible.
Thanks!
If you want to execute your own JavaScript function that exists in your test page from Selenium IDE, you need to make sure you access it via the window object. If you look at the reference for storeEval for instance, it says:
Note that, by default, the snippet will run in the context of the
"selenium" object itself, so this will refer to the Selenium object.
Use window to refer to the window of your application, e.g.
window.document.getElementById('foo')
So if you have your own function e.g. myFunc(). You need to refer to it as window.myFunc().
This can be very handy for exercising client-side validation without actually submitting the form, e.g. if you want to test a variety of invalid and valid form field values.
If you use runScript, that should already run in the window's context.
This works for me.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("myJavascriptGoesHere");
Make sure your javascript works first before using it here!
Actually to access your page javascript space, you need to get the real window of your page : this.browserbot.getUserWindow()
See this statement to get the jQuery entry point in your page (if it has jQuery of course ^^ )
https://stackoverflow.com/a/54887281/2143734
I'm a bit new to javascript. Is there a way to do what I am describing in the title completely client-side and without any external libraries? Or is using jQuery the best/only way to go?
You can import a json file from a server via AJAX and them simply eval it. You don't need a library for that but using one makes it a lot easier. Of course just evaling a json string is not very secure as it can contain arbitrary text so all libraries parse it to see if it's well formed etc.
EDIT:
If you want to learn about AJAX you can start with this tutorial from w3schools. Ajax stands for Asynchronous Javascript And XML and it allows you to send a request to the server without reloading the whole page. In your case you will not be using Xml but JSON. Anyway, the tutorial explains the whole idea.
Yes there is. You can use the "document.write" to add scripts to the DOM at runtime:
in your case:
document.write('<script ...></script>');
Basically you are adding the script tag to the dom that will request the new file.
However there is something else to consider, although the script will be downloaded, you will need to have a variable assignment in it in order to use it in your page:
var x = { //json object };