Get values from string using jquery or javascript - javascript

I have string like this:-
var src=url("http://localhost:200/assets/images/eyecatcher/6/black6.png)"
And now I want to get image name i.e black6.png and folder name 6.
I know there is substr function I can use but the file name and folder name will be dynamic like
orange12.png and 12 etc.
How I can get these values? Please help me.
Thanks

You can use split method for this:
var src = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var parsed = src.split( '/' );
console.log( parsed[ parsed.length - 1 ] ); // black6.png
console.log( parsed[ parsed.length - 2 ] ); // 6
console.log( parsed[ parsed.length - 3 ] ); // eyecatcher
etc.

If the base URL is always the same you could do
var url = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var bits = url.replace("http://localhost:200/assets/images/eyecatcher/", "").split("/");
var folder = bits[0], // 6
file = bits[1]; // black6.png

If your string is:
var src="http://localhost:200/assets/images/eyecatcher/6/black6.png"
Use the following:
var parts = src.split('/');
var img = parts.pop(); //black6.png
var flder = parts.pop(); //6
var sflder = parts.pop(); //eveatcher

You may try like this:
var str = myString.split('/');
var answer = str[str.length - 1];
var answer1 = str[str.length - 2];
var answer2 = str[str.length - 3];

var img_name = src.split('/')[7];
var folder_name = src.split('/')[6];

Using split & slice, Say like bellow
var src = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var arr = src.split('/').slice(-2) //returns ["6", "black6.png"]
arr[0] //folderName
arr[1] //filename

var str = "http://MachineName:200/assets/images/eyecatcher/6/black6.png";
var newStr = str.split("/");
ubound = newStr.length;
fileName = newStr[ubound-1];

Related

concat two variable and find array value position using javascript

I have array object like this below
var TTP01[2,0,0,0,0,0,4,6,1,4,0,9,1]
If I assign TTP01[0] like this, I will get Output 2. This is working fine.
But I'm getting values separately and I need to assign the Object.
object = TTP;
count =01;
xy = x*y;
I concat like this below
var obj = objname.concat(count, "[", xy, "]");
console.log( obj );
In console log, I'm getting like this TTP01[0].
But want to get output 2
Please help me... Thanks
This will work.
eval(objname + count)[xy]
fullcode:
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = "TTP";
var count = "01";
var xy = 0;
console.log(eval(objname + count)[xy]); // 2
You can try like this way,
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = 'TTP';
var count = '01';
xy = 0;
var obj = window[objname + count];
console.log( obj[xy] );
Assign TTP01 to some base object :
var base = {
TTP01: [2,0,0,0,0,0,4,6,1,4,0,9,1]
}
var objname = 'TTP';
var count = '01';
var objStr = objname + count;
var xy = 0;
console.log(base[objStr][xy])

Separate string into multiple variables with JavaScript

Using JavaScript I want to take a string like this var hashStr = 'modal-123456' and assign the string left of the - to a variable and the string right of the - to another variable.
If the string does not contain a - then ignore it.
How can I best achieve this?
var hashStr = location.hash.replace('#', '');
// hashStr now === 'modal-123456'
var firstHalf = // modal
var secondHalf = // '123456'
You can use split API.
var hashStr = 'modal-123456'
var splitStr = hashStr.split('-');
console.log(splitStr[0])
console.log(splitStr[1])
Just use split.
var hashStr = 'modal-123456';
var [firstHalf, secondHalf] = hashStr.split("-");
console.log("first half:", firstHalf);
console.log("second half:", secondHalf);
Simply
var hashStr = location.hash.replace('#', '');
var firstHalf = hashStr.split("-")[0];
var secondHalf = hashStr.split("-")[1];
or
var hashStr = location.hash.replace('#', '').split("-");
var firstHalf = hashStr[0];
var secondHalf = hashStr[1];

How do I grab any segment from URL using Javascript?

I have a URL : http://localhost:8080/school/teacher/reports/chapter-quiz/student
Getting the last segment, I just need to do this
var lastSegment = location.pathname.split('/').pop();
But how do I grab the one next to the last one ? chapter-quiz
I'd say something like this?
var segments = location.pathname.split('/');
secondLastSegment = segments[segments.length - 2];
Split the segments
var pathArray = window.location.pathname.split( '/' );
Create Variables
var segment_1 = pathArray[1];
var segment_2 = pathArray[2];
var segment_3 = pathArray[3];
var segment_4 = pathArray[4];
Use it
console.log(segment_4) --> chapter-quiz
you can use this
var t = window.location.pathname.split("/")
t.pop();
t.pop();
t.pop();

Required Specific Part of string using Jquery or JavaScript?

I want Require to Specific Part of string using Jquery or JavaScript?
Example :
var name = "jayesh.photo.png";
var name2 = "ruchita.photo.jpeg";
var name3 = "anil.photo.jpg";
How to get image extension using Jquery or JS.
I want ext1 = png, ext2 = jpeg, ext3 = jpg ;
You can use split() with .(dot) as delimitor and take the last array element after split.
var name = "jayesh.photo.png";
var name2 = "ruchita.photo.jpeg";
var name3 = "anil.photo.jpg";
var nameArray = name.split('.');
alert(nameArray[nameArray.length-1]);
var name2Array = name2.split('.');
alert(name2Array[name2Array.length-1]);
var name3Array = name3.split('.');
alert(name3Array[name3Array.length-1])
You could achieve this multiple ways:
var name = "jayesh.photo.png";
//1
console.log(name.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1"));
//2
console.log(name.substring(name.lastIndexOf(".")+1, name.length));
//3
var testvar = name.split('.');
console.log(testvar[testvar.length-1]);
//4
console.log(name.split('.').pop());
You can use String functions like below:
var required_string = name.substring(name.lastIndexOf(".")+1,name.length());
You could populate an array and loop it:
var name = "jayesh.photo.png";
var name2 = "ruchita.photo.jpeg";
var name3 = "anil.photo.jpg";
var photo = [name, name2, name3];
$.each(photo, function(key, value) {
console.log( "extension = " + value.substring(value.lastIndexOf("."),value.length ) );
});

Regex to get last word in URL between / and /

How do I get the last word in a URL that is URL between / and / ?
For example:
http://mywebsite.com/extractMe/test
http://mywebsite.com/extractMe
http://mywebsite.com/settings/extractMe/test
http://mywebsite.com/settings/extractMe
Here I would want to get extractMe from the URL.
If the URL is consistent, why not just use:
// Option 1
var url = "http://mywebsite.com/extractMe/test";
var extractedText = url.split("/")[3];
​// Option 2
// If when a trailing slash is present you want to return "test", use this code
var url = "http://mywebsite.com/extractMe/test/";
var urlAry = url.split("/");
var extractedText = urlAry[urlAry.length - 2];
​// Option 3
// If when a trailing slash is present you want to return "extractMe", use this:
var url = "http://mywebsite.com/extractMe/test/";
var urlAry = url.split("/");
var positionModifier = (url.charAt(url.length-1) == "/") ? 3 : 2;
var extractedText = urlAry[urlAry.length - positionModifier];
Here's a working fiddle: http://jsfiddle.net/JamesHill/Arj9B/
it works with / or without it in the end :)
var url = "http://mywebsite.com/extractMe/test/";
var m = url.match(/\/([^\/]+)[\/]?$/);
console.log(m[1]);
output:
test
This accounts BOTH for URLS like http://mywebsite.com/extractMe/test and http://mywebsite.com/extractMe/
function processUrl(url)
{
var tk = url.split('/');
var n = tk.length;
return tk[n-2];
}
Edited.
Regular Expression way:
var str = "http://example.com/extractMe/test";
var match = str.match(/\/([^\/]+)\/[^\/]+$/);
if (match) {
console.log(match[1]);
}

Categories