I am having some issues with the simple .replace() function from JS.
Here is my code
console.log(URL);
URL.replace("-","/");
console.log(URL);
Here is my output:
folder1-folder2-folder3
folder1-folder2-folder3
the second one should be
folder1/folder2/folder3
right?
If you guys need from my code, please let me know :)
Thanks in advance,
Bram
The correct thing is to replace it with a global regex with g after the regex that in this case is /-/
console.log(URL);
URL = URL.replace(/-/g,"/");
console.log(URL);
Replace returns a new string after replacement. It does not alter the string that replace was called on. Try this:
console.log(URL);
URL = URL.replace("-","/");
console.log(URL);
To replace all occurences look at this How to replace all occurrences of a string in JavaScript?
console.log(URL);
URL = URL.replace(/-/g, '/');
console.log(URL);
Related
I am trying to convert my parameters into a string in order to send an api request.
However, my current function replaces the spaces in the parameters into '%20'. How do I change it to change spaces into +?
Example params:
{name: "joe walsh"}
Current result:
...endpoint?name=joe%20walsh
Wanted result:
...endpoint?name=joe+walsh
Here's my current function
stringifyParams(params: any) {
return queryString
.stringify(params)
.replace(/[^?=&]+=(&|$)/g, '')
.replace(/&$/, '')
}
You can use Regex to replace %20 with +.
Append this to the function:
.replace(/%20/g, '+');
Firs you should decode your url with decodeURIComponent and then replace space with plus symbol
Try this peace of code and please let me know if that was useful for you)
const param = "endpoint?name=joe walsh";
console.log(decodeURIComponent(param.replace(" ","+")));
I have this URL string:
http://jackson/search/page/3/?features=Sea%20View&submit=search
Now I want to remove this part from ul: page/3/ when page is reload.
I am not good with jQuery and regex so I will appreciate if you help me.
Note: Page number can be anything form 1 to 100.
Thanks.
Have your tried using String.replace()
var url = "http://jackson/search/page/3/?features=Sea%20View&submit=search";
url = url.replace(/page\/\d+\//, '');
\d match any digit character:
url = 'http://jackson/search/page/3/?features=Sea%20View&submit=search';
url.replace(/page\/\d+\//, '')
// => "http://jackson/search/?features=Sea%20View&submit=search"
Try something like this,
var a='http://jackson/search/page/3/?features=Sea%20View&submit=search'.split('/');
console.log(a.slice(0,4).join('/')+'/'+a.slice(6).join('/'));
Fiddle Demo
You can use this Javascript:
if (location.href.indexOf("/page/") > -1) {
location.assign(location.href.replace(/\/page\/\d+\//, "/"));
}
I have a URL like
server/area/controller/action/4/?param=2"
in which the server can be
http://localhost/abc
https://test.abc.com
https://abc.om
I want to get the first character after "action/" which is 4 in the above URL, with a regex. Is it possible with regex in js, or is there any way?
Use regex \d+(?=\/\?)
var url = "server/area/controller/action/4/?param=2";
var param = url.match(/\d+(?=\/\?)/);
Test code here.
Using this regex in JavaScript:
action/(.)
Allows you to access the first matching group, which will contain the first character after action/ -- see the examples at JSFiddle
This way splits the URL on the / characters and extracts the last but one element
var url = "server/area/controller/action/4/?param=2".split ('/').slice (-2,-1)[0];
i want to break a following url
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324
into this by eliminating last two parametes i.e. &cn=bella&cjid=3344324
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434
the length of the url may change but the last two parameters remains in that position only. so how can i remove that in a efficient way.
A RegExp is the easiest way for this case:
str = str.replace(/&[^&]*&[^&]*$/,'');
You can use replace with regular expression. If the url is in var url then you can use this one
var new_url = url.replace(/&cn=.*/, '');
you can test it with
var url = 'http:\www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324';
console.info(url.replace(/&cn=.*/, ''));
var string = "http://dom.com/?one=1&two=2&three=3&four=4";
string.match(/(.*)&(.*)&(.*)/)[1]; // strips last two parameters
You can use regular expressions to replace the last 2 parameters with the empty string:
var url = "http://www.example.com/?p1=1&p2=2&p3=3&p4=4";
var urlWithoutLast2Parameters = url.replace(/&[^&]+&[^&]+$/,"");
You could use the function IndexOf to find the location of the '&cn' and then just use the substring function to create a new string eliminating the '&cn' portion of the URL, so something like...
var intIndexOf = str.IndexOf('&cn=')
strURL = strURL.substring(0,intCharAt)
I want to remove this from a url string
http://.....?page=1
I know this doesn't work, but I was wondering how you would do this properly.
document.URL.replace("?page=[0-9]", "")
Thanks
It seems like you want to get rid of the protocol and the querystring. So how about just concatenating the remaining parts?
var loc = window.location;
var str = loc.host + loc.pathname + loc.hash;
http://jsfiddle.net/9Ng3Z/
I'm not entirely certain what the requirements are, but this fairly simple regex works.
loc.replace(/https?\:\/\/([^?]+)(\?|$)/,'$1');
It may be a naive implementation, but give it a try and see if it fits your need.
http://jsfiddle.net/9Ng3Z/1/
? is a regex special character. You need to escape it for a literal ?. Also use regular expression literals.
document.URL.replace(/\?page=[0-9]/, "")
The answer from #patrick dw is most practical but if you're really curious about a regular expression solution then here is what I would do:
var trimUrl = function(s) {
var r=/^http:\/\/(.*?)\?page=\d+.*$/, m=(""+s).match(r);
return (m) ? m[1] : s;
}
trimUrl('http://foo.com/?page=123'); // => "foo.com/"
trimUrl('http://foo.com:8080/bar/?page=123'); // => "foo.com:8080/bar/"
trimUrl('foobar'); // => "foobar"
You're super close. To grab the URL use location.href and make sure to escape the question mark.
var URL = location.href.replace("\?page=[0-9]", "");
location.href = URL; // and redirect if that's what you intend to do
You can also strip all query string parameters:
var URL = location.href.replace("\?.*", "");