I am try to create a regular expression for a javascript project that covers every hyperlink that starts with "file://"
Thanks :)
Hyperlinks have location properties-
you can read document.links[0].protocol from a link.
var links=document.links, L=links.length, filelinks=[];
while(L){
if(links[--L].protocol=='file:')filelinks.push(links[L].href);
}
//eg:
filelinks.join('\n')
file:///C:/webworks/gallery/gallery.html
file:///C:/webworks/library/shared/dewey.html#holmes
file:///C:/webworks/library/shared/dewey.html#twain
file:///C:/webworks/library/shared/dewey.html
Try this one
/^(file?://)?([\da-z.-]+).([a-z.]{2,6})([/\w .-])/?$/
Enjoy :)
/^file\/\/:/i.test(str)
str.match(/^file\/\/:\S+/i)
I wouldn't check for any more than the protocol, URIs are very complicated.
Here is a very thorough version developed by this guy:
(?i)\b((?:file:(?:\/{2}))(?:www\d{0,3}[.]|[a-z0-9\-]+[.])?(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
EDIT: Evidently I needed to change a few things to make it more strict. I've updated it. If you want to use the capturing groups, remove the ?: to keep a backreference.
Please use the simpler, /(file:\/{2,3}[!#$&-;=?-\[\]_a-z~]+)/
Related
I need to make this into a string in java:
<script type="text/javascript">document.write("<img src=\"UpArrow.png\" /> \"); </script>
Can someone help? I keep trying and it ends up like this...
return "<script type=\"text/javascript\">document.write(\"<img src=\"UpArrow.png\" /> \"); </script>";
Which doesn't work because I need to double escape the quotes before and after UpArrow.png. since it needs to be escaped in javascript and not in java.
.
.
2019 Update: If you are looking at this, god help your soul. This is awful code and if you're trying to do things this way you're doing it wrong (As others suggested to me).
The correct way to do this would be jquery or one of the zillion DOM-modifying frameworks that exist now and popping stuff into / out of the scope of the DOM.
If you are doing this, you should not look at the code above or the solutions below, but should instead go learn more, as this is a path to make spaghetti code.
Apache commons have a methods just for this in StringEscapeUtils : the escapeJavaScript method.
Looks like it was moved in Apache Commons Lang 3 to ESCAPE_ECMASCRIPT in StringEscapeUtils.
https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/src-html/org/apache/commons/lang3/StringEscapeUtils.html#line.74
It seems it moved yet again, now it is part of "commons-text" and is named:
StringEscapeUtils.escapeEcmaScript
But good it still exists.
i need your help with an regex in Javascript, again.
Sometimes i have empty p-Tags in the DOM and i will remove them all.
Some look like:
<p></p>
others like:
<p> </p>
I think for an regex pro is this very lame, but i'm not good with it :(
so thanks in advance :)
You don't need to use regex for something this simple. Most likely you need to iterate over all the 'p' elements anyway. Something like this works:
http://jsfiddle.net/mqchen/3pV2P/
$('p').each(function(index, item) {
if($.trim($(item).text()) === "") {
$(item).slideUp(); // $(item).remove();
}
});
A very basic regex for this:
<p>\s*<\/p>
And here's a simple RegEx Tester for Firefox that might help you in the future. I use it quite a bit.
that's some methods that helps sometimes
$('p:empty').hide()
p:empty { display: none; }
The accepted answer is of course completely correct but since the poster asked for how to use regular expressions, here is a regex version (for completeness if you will) that does just that:
$('p').map(function() {
if( /^[\s ]*$/.test($(this).text()) ) {
$(this).remove();
}
});
Note a better regex version can be achieved by using the jQuery regex selector extension
I'm currently using this code (along with Mootools) to build an array of all anchors in the #subnav div that contain a specific url string:
$('subnav').getElements('a[href*=/'+href+']')
Problem is if I'm looking for work.aspx?subsection=24&project=1 that will match anchors with a URL of work.aspx?subsection=24&project=15.
How can I prevent that from happening?
The only solution I can think of is to use regexes; something like this:
$('subnav').getElements('a').filter(function (element, index) {
return /work\.aspx\?subsection=24&project=1(?:$|&)/.test(element.href);
});
That regex will match project=1 and then either a & or the end of the string. It uses MooTool's (or the native browser's) Array.filter to remove all non-matches. It isn't the most elegant solution, especially if you are using this dynamically, since then you'd have to write some code to create a new regex.
Going to answer my own question. It was as simple as changing the * to a $.
$('subnav').getElements('a[href$=/'+href+']')
Sounds like this should be handled more gracefully by a 301 or 302 redirect...
We have a javascript function we use to track page stats internally. However, the URLs it reports many times include the page numbers for search results pages which we would rather not be reported. The pages that are reports are of the form:
http://www.test.com/directory1/2
http://www.test.com/directory1/subdirectory1/15
http://www.test.com/directory3/1113
Instead we'd like the above reported as:
http://www.test.com/directory1
http://www.test.com/directory1/subdirectory1
http://www.test.com/directory3
Please note that the numbered 'directory' and 'subdirectory' names above are just for example purposes and that the actual subdirectory names are all different, don't necessarily include numbers at the end of the directory name, and can be many levels deep.
Currently our JavaScript function produces these URLs using the code:
var page = location.hostname+document.location.pathname;
I believe we need to use the JavaScript replace function in combination with some regex but I'm at a complete loss as to what that would look like. Any help would be much appreciated!
Thanks in advance!
I think you want this:
var page = location.href.substring(0,location.href.lastIndexOf("/"));
You can use a regex for this:
document.location.pathname.replace(/\/\d+$/, "");
Unlike substring and lastIndexOf solutions, this will strip off the end of the path if it consists of digits only.
What you can do is find the last index of "/" and then use the substring function.
Not sure you need a regex if you're just pulling off the last slash + content.
http://www.w3schools.com/jsref/jsref_lastIndexOf.asp
I'd probably use that to search for the last "/" character, then do a substring from the start of the string to that index.
How about this:
var page = location.split("/");
page.pop();
page = page.join("/");
I would think you need to use the .htaccess with rewrite rules to change the look of the url, however I am still looking to see if this is available to javascript. Will repost when I find out more
EDIT*
the lastIndexOf would only give you the position, therefor you would still need to replace. ex:
var temp = page.substring(page.lastIndexOf("/"),page.length-1);
page = page.replace(temp, "");
unfortunately I'm not that advanced in my coding so there is probably more efficient coding in the other answers. Sorry for any inconveniences with my initial answer.
Say I have a variable
tab_something
I need to drop the tab_ bit.
In php it's easy, using str_replace...
Will
tabSelect = document.location.hash.substr(1,document.location.hash.length);
(which would always be tab_something)
document.write(tabSelect.replace(/tab_/i, ""));
Work the way I would like it to, consistently across all modern browsers (ie6+) ?
Cheers.
Abusing source code rewrite as a substitute for reflection is … possible. I hate to state the obvious, but: maybe take a step back and see if you can reshape the project a bit, such that you can come up with a cleaner solution?
A couple of things:
document.location will be deprecated at some point by document.URL, consider using window.location.
Consider also using String.substring, since it is part of the ECMA-262 Spec.
var tabSelect = window.location.hash.substring(1); // remove "#"
tabSelect = tabSelect.replace(/tab_/i, ""); // remove "tab_"
It will work on old and modern browsers.
If document.location.hash always contains tab_ + some other string that you wish to retrieve, why not take advantage of the prefix always being the same length? You already have call substring() so why not let this function cut of a few more chars?
window.location.hash.substring(5)
Thanks to CMS for pointing out that window.location is preferred to document.location.
Yes it will. And also note that you don't have to use regular expressions in .replace(), .replace('tab_', ''); will do just fine.
Yes that is a standard JavaScript function that's been around long enough to cover all modern browsers ie6 and above. Should work just fine.
You could also use substring if you know it will always be the first 4 characters. tabSelect.substring(4) will give you everything starting the first character after tab_ (it is 0-based).