Regular expression for getting HREF from footer - html

I have a requirement where I need to get the last HREF in the HTML code, means getting the HREF in the footer of the page.
Is there any direct regular expression for the same?

No regex, use the :last jQuery selector instead.
demo :
foo
bar
var link = $("a:last");

You could use plain JavaScript for this (if you don't need it to be a jQuery object):
var links = document.links;
var lastLink = links[links.length - 1];
var lastHref = lastLink.href;
alert(lastHref);
JS Fiddle demo.
Disclaimer: the above code only works using JavaScript; as HTML itself has no regex, or DOM manipulation, capacity. If you need to use a different technology please leave a comment or edit your question to include the relevant tags.

It's not a good idea to parse html with regular expressions. Have a look at HtmlParser
to parse html.

Related

How do I get Mithril.js v0.2.5 to render raw HTML extracted from json? [duplicate]

Suppose I have a string <span class="msg">Text goes here</span>.I need to use this string as a HTML element in my webpage. Any ideas on how to do it?
Mithril provides the m.trust method for this. At the place in your view where you want the HTML output, write m.trust( '<span class="msg">Text goes here</span>' ) and you should be sorted.
Mithril it's powerfull thanks to the virtual dom, in the view you if you want to create a html element you use:
m("htmlattribute.classeCss" , "value");
So in your case:
m("span.msg" , "Text goes here");
Try creating a container you wish to hold your span in.
1. Use jQuery to select it.
2. On that selection, call the jQuery .html() method, and pass in your HTML string.
($('.container').html(//string-goes-here), for example)
You should be able to assign the inner HTML of the container with the string, resulting in the HTML element you want.
Docs here.

HTML & CSS - Shortcut URL

I wonder how to make shortcut URL in html and css (if it is possible)
I mean this:
HTML URL1
CSS #URL1 { URL('nextpage.html');}
Is this somehow possible?
Thank you for your answers.
So it can't work like this?
var url1 = "http://www.google.com";
Google
Is there any option how to do it?
To give you the short answer, no, you can't achieve this only by using CSS and HTML.
Cascading Style Sheets (CSS) is a style sheet language used for describing the look and formatting of a document written in a markup language. While most often used to change the style of web pages and user interfaces written in HTML and XHTML, the language can be applied to any kind of XML document, including plain XML, SVG and XUL. (...read more)
Also, here is an introduction to what CSS is and how to use it.
I recommend you also take a look at HTML Links.
To not be all negative, I will give you some solutions, you could use .attr and do it like this :
SCRIPT
$('#firstlink').attr("href", "http://google.com")
HTML
This will take you to google
...or you could do it like this:
SCRIPT
var url = document.getElementById("firstlink")
url.setAttribute("href", "http://google.com")
HTML
This will take you to google
It can't be possible. As css is for only styling the html.
Yes you can target particular url to style it.
Check out this page for further detail.
Or you can do it with php.
<?php
$url = 'example.com' ;
?>
<a href="<?php echo $url; ?>" >
click here
</a>

Extracting metadata from Web Pages [duplicate]

I was wondering if there's a way in javascript that allows me to process the html source code that allows me to take out specific tags that I want?
Sorry if it sounds easy or too simple. i am new to programming.
If you have the HTML in a string, then you can use:
var str = '<html></html>'; // your html text goes here
var div = document.createElement('div');
div.innerHTML = str;
var dom = div.firstChild; // dom is the object you want,
// you can manipulate it using standard dom methods
Alternately, use jQuery. jQuery is a library to help you manipulate and access HTML elements more easily. First, add this to the head of your document:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
This is a reference to the jQuery library. Then, do:
var foo = $("<html>Your html here</html>");
Or, if your html is in a variable (e.g. str), you can do:
var foo = $(str);
Then, you can manipulate and parse foo in a number of ways. For example, to remove all paragraph elements, you would use
foo.remove('p');
Or, to remove the paragraph element with id="bar", use:
foo.remove('p.bar');
Once you are done your modifications, you can get the new html text using:
foo.html();
Why is your html in a string? Is it not the html of the current page?
Use DOM it can pull data from webpages if you know the structure.

Pass parameter and use custom attributes?

I have the following code in my view:
<apex:commandLink data-role="button" action="{!someAction}">
<apex:param name="someVar" value="someVarVal">
</apex:commandLink>
The code does not work because commandLink does not take the "data-role" attribute.
How can I pass a parameter like I am with the second line, but also have an attribute like "data-role" on the rendered link?
I've never found a way to put extra attributes into VF tags, your best bet IMO would be to add some javascript which runs on page load and then use JQuery's .attr() method to add the attribute to the component.
Something like the following (assuming you've included jquery through a static resource)
<apex:commandLink styleClass="myLink" action="{!someAction}">
<apex:param name="someVar" value="someVarVal"/>
</apex:commandLink>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function(){
jQuery(".myLink").attr("data-role", "button");
});
<script>
You'll notice I've used a class instead of ID here, this is simply because SFDC generated IDs include symbols which need to be escaped for use with jQuery, and I find this is just the easier and cleaner solution to use (albeit probably slightly slower).
you can just use html pass through syntax :
html-attribute=value
for example
html-class="toto" will output class="toto"

How do you parse a web page and extract all the href links?

I want to parse a web page in Groovy and extract all of the href links and the associated text with it.
If the page contained these links:
Google<br />
Apple
the output would be:
Google, http://www.google.com<br />
Apple, http://www.apple.com
I'm looking for a Groovy answer. AKA. The easy way!
Assuming well-formed XHTML, slurp the xml, collect up all the tags, find the 'a' tags, and print out the href and text.
input = """<html><body>
John
Google
StackOverflow
</body></html>"""
doc = new XmlSlurper().parseText(input)
doc.depthFirst().collect { it }.findAll { it.name() == "a" }.each {
println "${it.text()}, ${it.#href.text()}"
}
A quick google search turned up a nice looking possibility, TagSoup.
I don't know java but I think that xpath is far better than classic regular expressions in order to get one (or more) html elements.
It is also easier to write and to read.
<html>
<body>
1
2
3
</body>
</html>
With the html above, this expression "/html/body/a" will list all href elements.
Here's a good step by step tutorial http://www.zvon.org/xxl/XPathTutorial/General/examples.html
Use XMLSlurper to parse the HTML as an XML document and then use the find method with an appropriate closure to select the a tags and then use the list method on GPathResult to get a list of the tags. You should then be able to extract the text as children of the GPathResult.
Try a regular expression. Something like this should work:
(html =~ /<a.*href='(.*?)'.*>(.*?)<\/a>/).each { url, text ->
// do something with url and text
}
Take a look at Groovy - Tutorial 4 - Regular expressions basics and Anchor Tag Regular Expression Breaking.
Parsing using XMlSlurper only works if HTMl is well-formed.
If your HTMl page has non-well-formed tags, then use regex for parsing the page.
Ex: <a href="www.google.com">
here, 'a' is not closed and thus not well formed.
new URL(url).eachLine{
(it =~ /.*<A HREF="(.*?)">/).each{
// process hrefs
}
}
Html parser + Regular expressions
Any language would do it, though I'd say Perl is the fastest solution.