For example i have such urls:
https://portalvhdsk7w6i7k9.blob.core.windows.net:443/project/5633cc12da73d9160c4cf146.dat
https://portalvhdsk7w6i7k9.blob.core.windows.com/project/5633cc12da73d9160c4cf146.dat
https://example.com/project/5633cc12da73d9160c4cf146.dat
can i somehow get only 5633cc12da73d9160c4cf146 using only js? is it possible?
You can split the string by / and the pop() the final element. Try this:
var url = 'https://portalvhdsk7w6i7k9.blob.core.windows.net:443/project/5633cc12da73d9160c4cf146.dat'
var hex = url.split('/').pop().replace('.dat', ''); // = '5633cc12da73d9160c4cf146'
If you want to get the location of the current page, use document.location.
Check this:
var url = "https://portalvhdsk7w6i7k9.blob.core.windows.net:443/project/5633cc12da73d9160c4cf146.dat"
var indexofProd = url.indexOf("project/");
indexofProd = indexofProd + 8;
var indexofdotDat = url.indexOf(".dat");
var number = url.substring(indexofProd,indexofdotDat);
alert(number);
Related
suppose i have 10 localstorage like blow:
localStorage.setItem("item01","this is a value/100");
localStorage.setItem("item02","this new is a value/110");
localStorage.setItem("item03","this is a good value/120");
localStorage.setItem("item04","this is a nice value/130");
I need a java script code to check if the key of for example item01 is not 0 then put the data of item01 before / to xyz and the data after / to rfv variable.
I would suppose you split the data-string along the character "/". Something like:
var lsData = localStorage.getItem("item01");
var dataArray = lsData.split("/");
if(dataArray.length === 2){
var xyz = dataArray[0];
var rfv = dataArray[1];
}
else{
... error-code (could not split exactly)...
}
This is an answer to your question "get value of string before and after / in javascript".
Like I stated in the comments you can split a string into an array of substrings using a delimiter.
jsfiddle
var str= "this is a value/100";
var temp=str.split("/");
var xyz = temp[0]; //->this is a value
var rfv = temp[1]; //->100
alert('xyz = '+xyz+'\nrfv = '+rfv);
this should do it...
I'm assuming you know how to get the localStorage value, so I'm just posting an example.
var a = "this is a value/100"
var b = /^(.*?)\/(\w+)/.exec(a);
var text = b[1]
var value = b[2]
The code seems to work fine when inputting numbers 1-9 but anything above doesn't work, what could be the issue? Here is the code:
var varkString = prompt('Enter your VARK scores - [visual|aural|read|kinesthetic]','9|3|11|10');
var subStrings = varkString.split('|');
var visual = varkString[0];
var aural = varkString[1];
var read = varkString[2];
var kinesthetic = varkString[3];
var varkBar = 30*visual
document.writeln('<img src="bar_blue.png" width='+varkBar+' height="25"/>');{
}
Edit: Solved
You are parsing first character when you are getting visual, second on aural and third on read.
I belive that you want to use subStrings
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
when you are slpiting the string varkString the array will automatically constructed and assigned to subStrings.so use it like this:
var subStrings = varkString.split('|');
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
var kinesthetic = subStrings[3];
I have tried googling this but can't find what I'm looking for. I have a url that has a number in it. I want to be able to take the number that is there and depending on what number is there then interject a name back into the url. For example:
Let's say the url is: www.example.com/video15637
Can I take that number and then do something like:
var nameVariable;
if(video15637){
nameVariable = video15637;
}
if(video26597){
nameVariable = video26597;
}
if(video18737){
nameVariable = video18737;
}
then, somehow interject the namevariable back into the url that is displayed?
You can try with:
var a = document.createElement('a');
a.href = 'http://www.example.com/video15637';
var nameVariable = a.pathname.substr(1); // video15637
You can simple use .split() or combination of .substr() and .lastIndexOf()
var url = 'www.example.com/video15637';
var video = url.split('/')[1];
alert(video)
OR
var url2 = 'http://www.example.com/video15637';
var video2 = url.substr(url.lastIndexOf('/') + 1);
alert(video2)
Combined DEMO
I have this piece of code:
var desconto = document.getElementById('desconto').value;
var portes = document.getElementById('portes').value;
var pr_equipamentos = document.getElementById('pr_total_equipamentos_escondido').value;
var pr_total;
pr_total = (pr_equipamentos * ((100-desconto)/100)) + portes;
pr_total = pr_total.toFixed(2);
alert(pr_total);
document.getElementById('pr_total_proposta').innerHTML = pr_total + " €";
The ID's desconto, portes and pr_total_equipamentos_escondido are input type in a form.
In this case I'm not able to use the toFixed(2). The first formula of pr_total gives me the number: 1324.7865372846 and the next step is not working (pr_total = pr_total.toFixed(2)).
What am I doing wrong?
When you define a var, in JavaScript it is a string, so you need to parse.
try:
pr_total = parseFloat(pr_total).toFixed(2);
_txt3.value = (parseFloat(t1) - parseFloat(t2));
_txt3.value = parseFloat(_txt3.value).toFixed(2);
lets say i have this variable
var image = "image.jpg";
I'm trying to split the content of the variable image and insert _thumbs into it to get something like image_thumbs.jpg.
How do i go about this?
Many Thanks.
function addSuffix(filename, suffix)
{
var pos = filename.lastIndexOf(".");
var left = filename.substring(0, pos);
var right = filename.substring(pos);
var result = left + suffix + right;
return result;
}
var image = "image.jpg";
var imageWithSuffix = addSuffix(image, "_thumbs");
// imageWithSuffix === "image_thumbs.jpg"
Or, just for fun, a much less readable but shorter solution using a regex:
function addSuffix2(filename, suffix)
{
return filename.replace(/\.[^\.]+$/, suffix + "$&");
}
var image = "image.jpg";
image = image.replace(".","_thumbs.");
Here is the solution
Way 1
var image = "image.jpg";
var splitVar = image.split(".");
alert(splitVar[0]);
alert(splitVar[1]);
alert(splitVar[0]+"_thumbs."+splitVar[1]);
Way 2
alert(image.replace(".","_thumbs."))
http://jsfiddle.net/LqpL3/1/
If the file name can contain multiple dots, you need to take the last of it. You can use Regex to do it:
var image = "image.jpg";
image = image.replace(/\.(?!.*\.)/, "_thumbs.");