I have an URL like this
localhost:8080/myroot/dir1/dir2/myTestpage.do
I need to get the string upto the root like this
var myURL = "localhost:8080/myroot/";
I tried the slicing but did not work:
var str = "localhost:8080/myroot/dir1/dir2/myTestpage.do";
var myURL = str.split("/").slice(-3, -2).toString();
How can I get the string upto "localhost:8080/myroot/" ?
A fiddle is here
How can I get that string from the url pattern?
This should work:
var str = "localhost:8080/myroot/dir1/dir2/myTestpage.do";
var myURL = str.split('/').slice(0,2).join('/') + '/';
Or more simply:
var myURL = str.split('/', 2).join('/') + '/';
Alternatively, you can use a regular expression like this:
var myURL = str.match(/[^\/]*\/[^\/]*\//)[0];
Or this:
var myURL = str.match(/.*?\/.*?\//)[0];
Modifying you var myUrl = line will make it work
var myURL = str.split("/").slice(0, 2).toString().replace(",", "/") + "/";
You can simply do this :
var myURL = str.split("/");
myURL = myURL[0] + '/' + myURL[1];
Fiddle link : http://jsfiddle.net/hnVnm/2/
And just to have a regex solution also:
"localhost:8080/myroot/dir1/dir2/myTestpage.do".match(/^(https?:\/\/)?[^\/]+\/[^\/]+\//)[0];
Fast and dirty:
var str = "localhost:8080/myroot/dir1/dir2/myTestpage.do";
var strSplit = str.split("/");
var myUrl = strSplit[0] + "/" + strSplit[1] + "/";
Try replace or match :
myURL = myURL.match(/^(.*?\/){2}/)[0];
myURL = myURL.replace(/^((.*?\/){2}).*$/, '$1');
// (.*?\/){2} : everything until a slash, 2 times
Adding a substring version of the answer:
var myURL = str.substring(0, str.indexOf('/', str.indexOf('/') + 1) + 1);
The key here is the second parameter of indexOf, which creates an offset to start searching from. The offset is also str.indexOf('/')+1, so it's finding the second forward slash as the end index for substring.
Related
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);
Not sure how valid is this approach, but I'm unable to split the string into 2 when there are repeated characters.
var match = 's';
var str = "message";
var res = str.split(match, 2);
For instance i tried to use split() on the string "message", it results into:
me,""
So i did this:
res = str.split(match, 3);
so now it resulted into:
me,,age
but as you can see im still missing the second 's' in the "message" string. what im trying to get is I'm passing a matched character (in above case var match which is dynamically generated) to the split() and splitting into 2. I was hoping to get something this:
res = me,,sage
is that possible using split() or is there a better method to achieve this?
P.S: in fiddle i've given another string eg: (string = "shadow") which works fine.
Fails only when there are repeated letters in the string!
fiddle: https://jsfiddle.net/ukeeq656/
EDIT::::::::::::
Thanks everyone for helping me out on this...and so sorry for last min update on the input, i just realized that var match; could be a word too, as in var match = 'force'; and not just var match ='s'; where the string is "forceProduct", so when my match is more than just a letter, this approach works: str.split(match, 2);, but str.indexOf(match); doesnt obviously... could there be an approach to split: "","Product". My extreme apologies for not mentioning this earlier.any help on this would be appreciated!!
eg fiddle:
https://jsfiddle.net/ukeeq656/3/
I don't think split() is the correct way to do this.
Please see below:
var match = 's';
var str = "message";
var index = str.indexOf(match);
var res =[];
res[0] = str.substring(0, index);
res[1] = " ";
res[2] = str.substring(index + 1);
console.log(res);
I'm not sure what your end goal is but I think this gets you what you want.
var match = 's';
var str = "message";
var index = str.indexOf(match);
var res = str.substring(0, index) + ',' + str.substring(index + 1);
alert(res); // me,sage
You could write a function to do this;
function newSplit(str, match) {
var num = str.indexOf(match);
var res = [];
res.push(str.substring(0, num));
//res.push(str.substring(num + 1, str.length)); // this line has been modified
res.push(str.substring(num + match.length, str.length));
return res;
}
var match = 'force';
var str = 'forceProduct';
console.log(newSplit(str, match));
This is what you want?
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);
var str = '#/promotionalMailer/test1';
output should be ==> #/promotionalMailer
I want the string before the second slash '/'
I have tried this so far:
var str = '#/promotionalMailer/test1';
var match = str.match(/([^\/]*\/){2}/)[0];
alert(match);
But it comes with the second slash.
try split, slice and join
var str = '#/promotionalMailer/test1';
console.log( str.split("/").slice(0,2).join("/"));
For example,
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.split('/').slice(0, 2).join('/')
document.write('<pre>'+JSON.stringify(result,0,3));
If you want regexes, then
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.match(/[^\/]*\/[^\/]*/)[0]
document.write('<pre>'+JSON.stringify(result,0,3));
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]);
}