This question already has answers here:
js function to get filename from url
(24 answers)
Closed 6 years ago.
Guys i have many links:
"img/dasda/ja.png", "../img/no.png", "wild/rpm/gulp/in.png",
How in js change sting to last "/"? To get just the name of image?
var url = "img/dasda/ja.png";
var idx = url.lastIndexOf('/');
console.log(url.substring(idx + 1));
You could use Array#match together with RegExp.
var links = ["img/dasda/ja.png", "../img/no.png", "wild/rpm/gulp/in.png"];
links.forEach(v => console.log(v.match(/(?!\/)\w+(?=\.)/g)[0]));
Related
This question already has answers here:
How to get URL parameter using jQuery or plain JavaScript?
(34 answers)
Closed 4 years ago.
I have this url
https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp
I need to get text of table name from the url when page loads.
I tried this
location.href.split('&')[1]
It returns "table=user". but I need the table name alone as user.
How can I get this?
Try using URL.searchParams
// location.href
let str = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp";
console.log((new URL(str)).searchParams.get("table"));
Split it once more:
location.href.split('&')[1].split('=')[1]
Or you could use Url class (no support in IE):
var url = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp"; //window.location.href
var url = new URL(url);
var table = url.searchParams.get("table");
console.log(table);
location.href.split('table=')[1].split('&')[0]
Don't assume the parameter order ;)
This question already has answers here:
Getting parts of a URL with JavaScript
(5 answers)
Closed 5 years ago.
url:
http://xxxxxx.com/video/view/12345
Can I take 12345 in the url using javascript?
Please help me
Use RegExp, Array#match and negative lookahead.
var str = 'http://xxxxxx.com/video/view/12345';
console.log(str.match(/(?!view\/)\d+/)[0]);
You can also try this if you're sure that it'll always be in last:
var num = location.pathname.split('/').pop(); // "12345"
and further: parseInt(num);
You can parse your URL with the following code. Then just get the last part.
var url = 'http://xxxxxx.com/video/view/12345';
var url_parts = url.replace(/\/\s*$/,'').split('/');
console.log(url_parts[url_parts.length - 1]); // last part
This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 6 years ago.
i have a url like this
test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14
i want to remove the parameter using the following javascript
function removeParam(uri) {
uri = uri.replace(/([&\?]start_date=*$|start_date=*&|[?&]start_date=(?=#))/, '');
return uri.replace(/([&\?]end_date=*$|end_date=*&|[?&]end_date=(?=#))/, '');
}
but it didn't work, anyone know what's wrong with that?
in modern browsers you can do this quite simply
var x = new URL(location.origin + '/test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14');
x.searchParams.delete('start_date');
x.searchParams.delete('end_date');
var uri = x.pathname.substr(1) + x.search; // substr(1) because you don't have a leading / in your original uri
at least, I think it's simpler
Your RegExp is no match!
If you want remove end_date, you should:
uri.replace(/(end_date=)([^*&]+)/, 'end_date=')
And so on.
This question already has answers here:
remove / replace unwanted prefixed info from `class` name
(2 answers)
Closed 7 years ago.
I am trying to update the style html - by replacing a string, but not working. i am not able to get the final updated html.
here is my try:
var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>"
html += $(html).html(function(_,h){
$(h).find('style').html().replace(/^\w+ (\.\w+)/gm,'$1');
});
console.log(html); //throws the error.
jsfiddle
It works for me..
var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>";
var $h = $('<div>'+html+'</div>');
$h.find('style').html(function(_,h){ return h.replace(/^\w+ (\.\w+)/gm,'$1'); });
var updated = $h.html();
console.log(updated);
Thanks every one!
This question already has answers here:
Get escaped URL parameter
(19 answers)
Closed 9 years ago.
Query string :
Pages/Service.aspx?id=4&comment=1
I am new to jquery ,Kindly help me out with a simple solution to get the Id and COMMENT in "var id & var comment" using jQUERY in asp.net C#
Use following code
<script>
function getQuery(key){
var temp = location.search.match(new RegExp(key + "=(.*?)($|\&)", "i"));
if(!temp) return.
return temp[1];
}
var id = getQuery('id');
var comment = getQuery('comment');
</script>