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]);
}
Related
Let's say that this is my URL/Link that i have written in an input
https://www.instagram.com/p/CBt-W4jHZjH/
How can I get the "CBt-W4jHZjH" part?
var link = ?????
var a = link.val().trim();
var regex = new RegExp(/^(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,29}$/);
var validation = regex.test(a);
https://developer.mozilla.org/en-US/docs/Web/API/URL
const getLastPath = (url) => {
url = new URL(url);
const pathname = url.pathname;
const paths = pathname.split("/");
return paths.pop() || paths.pop();
}
console.log(getLastPath("https://www.instagram.com/p/CBt-W4jHZjH/")); // "CBt-W4jHZjH"
console.log(getLastPath("https://www.instagram.com/p/CBt-W4jHZjH")); // "CBt-W4jHZjH"
Many ways to do it. One way is to look for / any character but / ending with / end of line.
var url = 'https://www.instagram.com/p/CBt-W4jHZjH/'
var x = new URL(url);
console.log(x.pathname.match(/\/([^\/]+)\/?$/)[1])
Could be done with split. The filter removes the empty string caused by the trailing /.
var url = 'https://www.instagram.com/p/CBt-W4jHZjH/'
var x = new URL(url);
console.log(x.pathname.split('/').filter(x=>x).pop());
I have a string like:
var str = put returns between paragraphs abc_start indent code by 4 spaces abc_end quote by placing > at start of line abc_start to make links abc_end.
I'm displaying this string in my browser using:
res.send('/page/+result);
I want to filter out result such that only the content which starts at abc_start and end at abc_end remains. How do I do that in node.js?
For eg: output:
abc_start indent code by 4 spaces abc_end abc_start to make links abc_end
I tried using : str.split('abc_start').pop().split('abc_end').shift();
But I'm not gettting desired output.Please help.
<script>
function myFunction() {
var str = "abc_start indent code by 4 spaces abc_end";
var n = str.indexOf("abc_start ");
var m = str.indexOf("abc_end");
var res = str.slice(n+9, m-1);//Since you already know the length of the string "abc_start" that you want out of the way
document.getElementById("demo").innerHTML = res;
}
</script>
Below is code snippet to solve this scenario:
var str = 'xxxadasyyydsdsxxxadadyyy';
var start = 'xxx';
var end = 'yyy'
var res = [];
find();
function find() {
var initialIndex = str.indexOf(start);
var lastIndex = str.indexOf(end);
if(initialIndex > -1 && lastIndex > -1 && initialIndex < lastIndex) {
res.push(str.substring(initialIndex, lastIndex + start.length));
str = str.substring(lastIndex + start.length);
find();
} else {
return;
}
}
console.log(res);
Why isn't this working!?
I'm trying to replace the '/' with '\/',
var string = "/tmp/fuse_d/DCIM/100MEDIA/YDXJ0044.mp4"
var param = string.replace(/\//g,'\/');
console.log(param) > /tmp/fuse_d/DCIM/100MEDIA/YDXJ0044.mp4
Here is a fiddle https://jsfiddle.net/6r3wye7b/
const x = str => str.replace(/\//g, '\\\/')
If you want to replace all / into \ or all / replace into \/ here is working demo for you.
var string = "/tmp/fuse_d/DCIM/100MEDIA/YDXJ0044.mp4"
var param = string.replace(/\//g,'\\');
var param2 = string.replace(/\//g,'\\/');
console.log(param);
console.log(param2);
I have a string like below
var indicator = -65(www.anyweb.com)
the number -65 can be any number too. How can I take out only the web url separately in javascript?
You need to extract the string after '(' and before ')'
var str = "-65(www.anyweb.com)";
str = str.substring(str.lastIndexOf("(")+1,str.lastIndexOf(")"));
You can use this example for string operations
var data = "-65(www.anyweb.com)";
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')')); console.log("URL :: ",url);
var domain = /\((.*?)\)/.exec("-65(www.anyweb.com)")[1];
console.log(domain);
The regex above will create a group with anything that's inside parenthesis.
You can use some simple string operations:
var str = "-65(www.anyweb.com)";
var url = "N/A";
// Find indices of open and close parentheses
var open = str.indexOf("(");
var close = str.lastIndexOf(")");
// If they were found then extract the URL from the string
if (open !== -1 && close !== -1) {
url = str.substring(open + 1, close);
}
console.log(url);
If you are more inclined to use regular expressions then this should do it:
var str = "-65(www.anyweb.com)";
var regex = /\((.*?)\)/; // Capture URL inside parentheses
var result = regex.exec(str); // Execute the regex against the string
var url = "N/A";
// If the URL was matched then assign it to the variable
if (result[1] !== undefined) {
url = result[1];
}
console.log(url);
You can also simply replace the stuff that you do not want:
var str = "-65(www.anyweb.com)";
str = str.replace(/^.*\(/, ""); // Remove everything before URL
str = str.replace(/\).*$/, ""); // Remove everything after URL
console.log(str);
Example 1 :
var data = "-65(www.anyweb.com)";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
Example 2 :
var data = "-65";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
Example 3 :
var data = "-65(www.anyweb.com)6764872";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
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];