Removing %20 value from get method - javascript

Removing %20 in get method?
var c=new Array(a);
(eg: a={"1","2"}) window.location="my_details.html?"+ c + "_";
and in my_details.html :
var q=window.location.search;
alert("qqqqqqqqqqqqq " + q);
var arrayList = (q)? q.substring(1).split("_"):[];
var list=new Array(arrayList);
alert("dataaaaaaaaaaaa " + list + "llll " );
and in "list" its dusplaying me "1%202";
How can I remove this %20 =space value ??
Thanks

just use this:
alert("dataaaaaaaaaaaa " + decodeURIComponent(list) + "llll " );
This should decode the %20 to space
look here: http://www.w3schools.com/jsref/jsref_decodeURIComponent.asp

If there is a space in the parameter(s), then the %20 (URL Encoding) is necessary. You cannot pass a space in a GET request.
If you need to avoid this, use POST.

As far as I can see the problem is being introduced at this line:
window.location="my_details.html?"+ c + "_";
This could be written as:
window.location="my_details.html?"+ c.toString() + "_";
The default .toString() of a JavaScript Array would be to use a delimiter of ,, i.e.
var str = ["1", "2", "3"].toString(); // 1,2,3
In you example it appears that the delimiter being used is a space. This would have been changed by something changing the default behaviour of .toString() on the Array.prototype. Try using the following:
window.location="my_details.html?"+ c.join(",") + "_";

Better to use replace() method to replace %20 to space
list.replace("%20"," ");

Related

Replace null bytes

I want to replace null bystes from string. But after replacing of the null bytes \u0000 of the string
let data = {"tet":HelloWorld.\u0000\u0000\u0000\u0000"}
let test = JSON.parse(data).tet.replace("\u0000", "");
I am getting always following value:
HelloWorld.[][][][]
This are not array brackets or something like that.
I just need the value HelloWorld. How can I do this?
Ok, the solution was to replace all bytes.
.replace(new RegExp("\u0000", "g"), "");
A normal string replace only replaces the first occurence.
But by using a regex with a global flag it'll replace all occurences.
Example snippet :
console.log("bar bar".replace("bar","foo"));
console.log("bar bar".replace(/bar/g,"foo"));
let data = {"test":"HelloWorld.\u0000\u0000\u0000\u0000"};
console.log("before: " + data.test);
console.log("before (stringified): " + JSON.stringify(data.test));
// removing all the NULL unicode characters
data.test = data.test.replace(/\u0000/g,'');
console.log("after (stringified): " + JSON.stringify(data.test));

Change data inside string with box brackets

I have a string
garments[0][1]; // The 0 and 1 can be other numbers
I need to replace the data inside the second and the third box brackets.
[0] and [1]
So that it can be
garments[4][6]
Please let me know your suggestions when you get a chance, thank you.
You can try that:
var string = 'garments[' + 4 + '][' + 6 + ']'; //in your onClick function
//To increment dynamically:
var string = 'garments[' + i + '][' + j + ']'; //i and j being variables incrementing in your loops/treatments
Update to address comments:
If you want to break "garnments[0][1]" into "garnments",0 and 1 you can do the following:
var string = "garnments[0][1]";
string = string.split('['); //string = [["garnments"],["0]"],["1]"]]
string[1].replace(']','');
string[2].replace(']',''); //string = [["garnments"],["0"],["1"]]
You can then change values and rebuild your string for further use.
It is a bit brutal though. You can use RegExp as showed by #Diego
You can use String.prototype.replace()
'garments[0][1]'.replace('[0]','[4]').replace('[1]','[6]')
For any possible string with ***[m][n] format:
Function SetNewValues(testString, n, m)
{
var keyWordLengh = testString.indexOf("[");
return testString.substring(0,keyWordLengh) + "[" + n.toString() + "][" + m.toString() + "]";
}
Where:
testString is entire string to work on, like "something[342][345]"
n,m are values to be put inside brackets :)
This would be my approach.
var string = "['foobar'][2][12]";
var match =
/\[([^\]]+)\](?:\[(\d+)\])(?:\[(\d+)\])/g
.exec(string);
console.log(match);

how to Remove the characters in between a string using index in jquery or javascript?

I want to remove the few characters in a string using index.
for example:
My input is: "5,4,3,2,1"
I want to remove the 1th to 2nd index position characters(here , to 4).
The Output should be 5,3,2,1.
Is there any predefined function in jquery or javascript to done this?
You can try to use substring function like this:
var mystring = "5,4,3,2,1";
alert( mystring.substr(0, 1) + mystring.substr(3));
JSFIDDLE DEMO
I would juse use javascripts split function for that.
So if you have
var string = "5,4,3,2,1";
than you just need to do
var splitted = string.split(",");
whereas the character in the brackets is the one you want to split on. After you did that, you can just make a new string, and build it with the array elements.
So you do something like
var string2 = splitted[0] + "," + splitted[2] + "," + splitted[3] + "," splitted[4];

append single quotes to characters

I have a string like
var test = "1,2,3,4";
I need to append single quotes (' ') to all characters of this string like this:
var NewString = " '1','2','3','4' ";
Please give me any suggestion.
First, I would split the string into an array, which then makes it easier to manipulate into any form you want. Then, you can glue it back together again with whatever glue you want (in this case ','). The only remaining thing to do is ensure that it starts and ends correctly (in this case with an ').
var test = "1,2,3,4";
var formatted = "'" + test.split(',').join("','") + "'"
var newString = test.replace(/(\d)/g, "'$1'");
JS Fiddle demo (please open your JavaScript/developer console to see the output).
For multiple-digits:
var newString = test.replace(/(\d+)/g, "'$1'");
JS Fiddle demo.
References:
Regular expressions (at the Mozilla Developer Network).
Even simpler
test = test.replace(/\b/g, "'");
A short and specific solution:
"1,2,3,4".replace(/(\d+)/g, "'$1'")
A more complete solution which quotes any element and also handles space around the separator:
"1,2,3,4".split(/\s*,\s*/).map(function (x) { return "'" + x + "'"; }).join(",")
Using regex:
var NewString = test.replace(/(\d+)/g, "'$1'");
A string is actually like an array, so you can do something like this:
var test = "1,2,3,4";
var testOut = "";
for(var i; i<test.length; i++){
testOut += "'" + test[i] + "'";
}
That's of course answering your question quite literally by appending to each and every character (including any commas etc.).
If you needed to keep the commas, just use test.split(',') beforehand and add it after.
(Further explanation upon request if that's not clear).

Parse JS only using JS

I have the below JS code which creates like (47.624718,-122.356133)
var point = new GLatLng(
parseFloat(markers[i].getAttribute(\"lat\")),
parseFloat(markers[i].getAttribute(\"lng\"))
);
To only make the value JUST 47.624718 -122.356133 ? how can I use the js replace() function to do so?
Just use the object methods:
point.lat() + ' ' + point.lng()
output = point.lat() + ' ' + point.lng();
See here: https://developers.google.com/maps/documentation/javascript/v2/reference#GLatLng
You can use 3 chained replace() invocations:
point = point.replace("(", "").replace(")", "").replace(",", " ");
It will replace the left and right parenthesis with an empty string, and the comma with a single space.

Categories

Resources