I have a variable urlthat I would like to insert into ".js"after the path part, if there is already a part after this path ".<something>".
So, here is an array with hard-coded start values and expected end values for each, which, we hope, illustrates the problem well:
var url_pairs = [
["http://example.com/foo", "http://example.com/foo.js"],
["http://example.com/foo/bars", "http://example.com/foo/bars.js"],
["http://example.com/foo/bars?quz=baz", "http://example.com/foo/bars.js?quz=baz"],
["http://example.com/foo/bars?quz=baz&qix=bax", "http://example.com/foo/bars.js?quz=baz&qix=bax"],
["/foo", "/foo.js"],
["/foo/bars", "/foo/bars.js"],
["/foo/bars?quz=baz", "/foo/bars.js?quz=baz"],
["/foo/bars?quz=baz&qix=bax", "/foo/bars.js?quz=baz&qix=bax"],
["/foo/bars.js?quz=baz&qix=bax", "/foo/bars.js?quz=baz&qix=bax"],
["/foo/bars.xml?quz=baz&qix=bax", "/foo/bars.xml?quz=baz&qix=bax"]
]
I'm struggling to come up with a clean and quick way to do this, but I'm sure it can be done cleanly and simply, for example, using a regular expression. Can someone set me up right? thanks max
EDIT : test solutions - here is a snippet that I wrote to test it.
function convertUrl(oldUrl){
}
$.each(url_pairs, function(i,pair){
var oldUrl = pair[0];
var expected = pair[1];
var newUrl = convertUrl(oldUrl);
if(newUrl != expected){
console.log("expected \""+expected+"\", but got \""+newUrl+"\"");
}
});