Removing comma for value with Javascript - javascript

thanks for looking my questions, I can't find the way to remove the comma with javascript. So I have problem at removing comma from page with java-script, Adding Comma for output is fine i guess, but for removing comma for value, the function is not working for me.
The source code is : http://jsfiddle.net/Q5CwM/2/
Please let me know, thanks again.

I have no idea what your code is trying to do overall, but you can fix this function that removes commas:
function checkNumeric(objName) {
var lstLetters = objName;
var lstReplace = lstLetters.replace(/\,/g,'');
}
by changing it to this:
function removeCommas(str) {
return(str.replace(/,/g,''));
}
You weren't returning the changed string from the function and I changed the name of the function and parameters to represent what it does.
str.replace returns the changed string. It does not change the string you started with so in order to do something with the result of the replacement, you have to either return that from the function or assign it to some other string. As you had it, nothing was happening with the replaced string so the function did nothing.

Related

How to use `%value%` in javascript?

I am learning JavaScript and I see %value% in a code but I do not know what does it mean or how to use it. Can anyone please help me explain to me. Thank you very much.
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
"%data%" is just a literal string. This code will take the value of HTMLWorkLocation, look for the first occurrence of %data% in it, and replace that with the value of work.jobs[job].location, and store the resulting string in formattedLocation.
var work = {
jobs: [{
location: "Home office"
}]
};
var job = 0;
var HTMLworkLocation = "John is located at %data%";
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
console.log(formattedLocation);
This is probably part of a template system that's used to replace placeholders like %data% with values that come from a table.
You're using string.replace which takes a string or regular expression as it's first argument. Based on the code you posted it looks like you're looking for the string "%data%" (or whatever string you're looking for) in HTMLworkLocation and replacing it with the value in work.jobs[job].location. Then it is being stored in formattedLocation.
I would put a debugger; line after that line of code so you can see what the values are in the debugger console. That might help make more sense of things.
Here is more info on the str.replace method with some examples

converting a string to float doesn't work

I got a variable Javascrpit which has a number as a string in this case 0.84. I'm trying to convert it into a float but when I try to it appears a 0 as float instead the 0.84.
I'm using this:
var pot="0.84";
var asd = parseFloat(pot);
console.log(asd);
EDIT:
This is not exactly the example. I recover data from the HTML and it works for other numbers but not for this. It is difficult to explain my problem exactly. It is a lot of code and works for other numbers so don't know exactly.
Your input is not "0.84". If you test with that, you will get the correct answer. Your input has something else inside, like spaces, for example:
"0 .84"
This should be the solution:
parseFloat(pod.replace(/ /g, ""))
I have tried this example on my end and it completely worked. However, you can try to instead input the string value directly into the parse float() function and it should print our your expected value. If you still want to assign the parsefloat() to a variable, then try to either rewrite the code or re-open your IDE because the code should work.
var pot = "0.84"
console.log(parseFloat(pot))
or you can just write it in one line
console.log(parseFloat("0.84"))

Javascript "Replace" not working on string returned via jQuery

I'm trying to convert a currency string to a number. I'm using a replace function with a regexp that I've used successfully in a similar context before.
The currency string is captured here, in part of an "each" loop:
var unitGridPrice = jQuery(this).find(".clsPriceGridDtlPrice").html();
The result is that unitGridPrice is a currency string, something like "$2.75". I'm trying to convert it to a number here:
var priceToConvert = unitGridPrice;
var unitGridPriceNo = Number(priceToConvert.replace(/[^0-9\.]+/g, ''));
However with that last line in place, the script will not run.
If I use the value of priceToConvert it correctly displays the currency text string, so I believe the string feeding the replace function is correct.
if I change "var priceToConvert = unitGridPrice" to "var priceToConvert = "$2.75" the script properly returns 2.75. I can copy and past the value that unitGridPrice displays into the text string I'm testing with and it works, but with the variable there the script dies.
I've tried removing the regex, changing the replace to .replace('$', '') and again the script stops with the variable in place but works if I test with a fixed string.
I'm really stumped. Help??!! Thank you!!!
i had some problem while try to get number from string also, little time ago. the problem is the regex, so i changed the regex like code below.
var id = element.name.replace ( /[^\d.]/g, '' );
element.name above is like input_21,input_22, etc. and i wanna get only the number(21,22).
hope it can help you. :)

Javascript: strip first and last \ from regExp variable

I'm cobbling together a script which maps mouse X,Y coordinates to an axis grid. The resulting variable will then be passed to CSS transform property. I'm getting all the numbers I need, but I'm stuck on the last part, which is to remove the \ around the result, which has been converted to a regular expression so as to allow for negative integers.
var resultX = RegExp(Math.round(mousePos.x/6.6) -60);
resultX = resultX.replace(/\//g,'');
The final stage (stripping the slashes) throws an error no matter how I do it. I've tried encapsulating .replace in a function, and using return, but I continue to get the same error:
TypeError: 'undefined' is not a function (evaluating
'resultX.replace(///g,'')')
I'm stuck, and haven't been able to find the solution anywhere. Perhaps the problem is that my variable isn't a true string? Or maybe someone has a suggestion for a better way to allow for negative integers.
JS fiddle:
http://jsfiddle.net/wAKnY/
Like JayC mentioned in the comments, there doesn't seem to be a reason to convert to a RegExp in the first place, so I would just recommend removing the enclosing RegExp().
However, if you require that for some reason that isn't apparent here, you can then call toString() on it to enable the replace function to behave correctly:
resultX = resultX.toString().replace(/\//g,'');
As JayC said, you don't need a regexp in the first place.
var resultX = (Math.round(mousePos.x/6.6)-60).toString;
This works:
resultX = resultX.toString().replace(/\//g, '');
resultY = resultY.toString().replace(/\//g, '');

Regex: find whatever comes after one thing before another thing

I want to find anything that comes after s= and before & or the end of the string. For example, if the string is
t=qwerty&s=hello&p=3
I want to get hello. And if the string is
t=qwerty&s=hello
I also want to get hello
Thank you!
\bs=([^&]+) and grabbing $1should be good enough, no?
edit: added word anchor! Otherwise it would also match for herpies, dongles...
Why don't you try something that was generically aimed at parsing query strings? That way, you can assume you won't run into the obvious next hurdle while reinventing the wheel.
jQuery has the query object for that (see JavaScript query string)
Or you can google a bit:
function getQuerystring(key, default_)
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
looks useful; for example with
http://www.bloggingdeveloper.com?author=bloggingdeveloper
you want to get the "author" querystring's value:
var author_value = getQuerystring('author');
The simplest way to do this is with a selector s=([^&]*)&. The inside of the parentheses has [^&] to prevent it from grabbing hello&p=3 of there were another field after p.
You can also use the following expression, based on the solution provided here, which finds all characters between the two given strings:
(?<=s=)(.*)(?=&)
In your case you may need to slightly modify it to account for the "end of the string" option (there are several ways to do it, especially when you can use simple code manipulations such as manually adding a & character to the end of the string before running the regex).

Categories