Replace / in URL in Javascript - javascript

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);

Related

replace first n occurrence of a string

I have a string var with following:
var str = getDataValue();
//str value is in this format = "aVal,bVal,cVal,dVal,eVal"
Note that the value is separated by , respectively, and the val is not fixed / hardcoded.
How do I replace only the bVal everytime?
EDIT
If you use string as the regex, escape the string to prevent malicious attacks:
RegExp.escape = function(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
new RegExp(RegExp.escape(string));
var str = "aVal,bVal,cVal,dVal,eVal";
var rgx = 'bVal';
var x = 'replacement';
var res = str.replace(rgx, x);
console.log(res);
Try this
var targetValue = 'bVal';
var replaceValue = 'yourValue';
str = str.replace(targetValue , replaceValue);

Javascript regex get string before second /

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 to get particular string from one big string ?

I have string
var str = "Ahora MXN$1,709.05" and wanted to get only
"MXN$1,709.05" from this.
Can someone please help me?
You can use substring or replace. With replace you are going to replace something with nothing.
replace
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.replace(sub,'');
substring
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.substring(sub.length);
JsFiddle
You can use either substring or Regex
Using substring
var str = "Ahora MXN$1,709.05";
var result = str.substring('Ahora '.length);
console.log(result);
Using Regex
var str = "Ahora MXN$1,709.05";
var myRegexp = /Ahora\s(.*?)(?:\s|$)/g;
var match = myRegexp.exec(str);
console.log(match[1]);

extract string with java-script

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.

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