Get values ​​from string using jquery or javascript

I have a line like this: -

var src=url("http://localhost:200/assets/images/eyecatcher/6/black6.png)"

And now I want to get the image name ie black6.png and folder name 6 . I know there is a substr function that I can use, but the file name and folder name will be dynamic like orange12.png and 12 etc.

How can I get these values? Please help me.

thanks

+5
source share
7 answers

If the base URL is always the same, you can 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 
+5
source

You can use the split method:

 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 

and etc.

+8
source

If your line 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 
+3
source

You can try the following:

 var str = myString.split('/'); var answer = str[str.length - 1]; var answer1 = str[str.length - 2]; var answer2 = str[str.length - 3]; 
+2
source
 var img_name = src.split('/')[7]; var folder_name = src.split('/')[6]; 
+2
source

Using split and slice , tell me how below

 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 
+2
source
 var str = "http://MachineName:200/assets/images/eyecatcher/6/black6.png"; var newStr = str.split("/"); ubound = newStr.length; fileName = newStr[ubound-1]; 
+2
source

Source: https://habr.com/ru/post/1200587/


All Articles