I'd suggest:
var url = "http://google.com/foo/querty?act=potato".split('/').pop(),
urlPart = url.slice(0,url.indexOf('?'));
console.log(urlPart);
I'd strongly suggest not using regular expressions for this, given the needless complexity (but that is, of course, a personal preference).
Edited to address the failure of the above to meet both test-cases shown in the question (it fails in the second case). The following handles both cases specified:
Object.prototype.lastStringBefore = function (char, delim) {
if (!char) {
return this;
}
else {
delim = delim || '/';
var str = this,
index = str.indexOf(char),
part = str.charAt(index - 1) == delim ? str.split(delim).slice(-2, -1) : str.split(delim).pop();
return part.length === 1 ? part[0] : part.slice(0, part.indexOf(char));
}
}
var url1 = 'http://google.com/foo/querty?act=potato',
url2 = 'http://google.com/foo/querty/?act=potato',
lastWord1 = url1.lastStringBefore('?', '/'),
lastWord2 = url2.lastStringBefore('?', '/');
console.log(lastWord1, lastWord2);
JS Fiddle demo.
References: