Anyone can helps me to extract decimal number from string using Ajax.
What i want to do:
Input string:
"Laptop,sno,67890,FAN" // This is complete input string with comma and decimal.
The output what i want :
67890 // only decimal without comma and any text.
I have use function of parseInt("input");
But it works only when the my input is start with decimal like 123,name but if the input is not start with decimal than it does not work it gives me NaN in result.
Any help in this regards would be highly appreciated.
use regular expresiion
var re = /\d+/;
var str = "Laptop,sno,67890,FAN";
alert (str.match(re));
DEMO
You can use Javascript Regular Expressions.
var s = "Laptop,sno,67890,FAN";
var decimals = s.match(/[0-9]+/);
decimals will be an array containing all the decimals in the string s.
You can use regular expression:
var str = 'Laptop,sno,67890,FAN';
var re = /(\d)+/i;
var found = re.exec(str);
console.log(found[0]);
FIDDLE
Related
How can I replace a decimal in a number with a string? For example, if I have a number 12.12, how can I take the decimal in that number and replace it with a comma (,) so that the output would be 12,12?
I tried this, but my app crashes because of it:
let number = 12.12
number.replace(/./g, ',');
Thanks.
You cannot use replace on a number, but you can use it on a string.
Convert your number to a string, and then call replace.
Also, the period (.) character has special meaning in regular expressions. But you can just pass a plain string to replace.
const numberWithCommas = number.toString().replace('.', ',');
try this:
var stringnumber = stringnumber.ToString();
var endresult = stringnumber.replace(".",",");
You cannot change the value of a const in javascript.
I wants to remove alphabet from string. In my string variable it will have the numbers with alphabet
For example
var myString = '1122D'
// I want remove the last alphabet only from the above variable
var myString = '1122Z3'
// I want remove the `Z3` from above string
var myString = '112DD2'
// I want remove the `DD2` from above string
I know how to replace specific character using .replace('',''). But in my case it is different
If the strings are always made up starting with numbers and you want to get the number up until the first alphabetical character, I'd recommend the use of parseInt() since its behaviour is exactly that it parses numeric characters in a string to a number until it encounters the first non-numeric character where it stops parsing.
var myNumber = parseInt(myString);
use this code:
myString.substr(0,myString.search('[a-zA-Z]'));
You may also do like
myString.replace(/[^\d].*/,"");
You can use regex /([\d]+).+$/g as well:
var regex = /([\d]+).+$/g;
console.log(regex.exec("1122D")[1]);
regex.lastIndex = 0;
console.log(regex.exec("1122Z3")[1]);
regex.lastIndex = 0;
console.log(regex.exec("112DD2")[1]);
Best way -
myString.slice(0, myString.indexOf(myString.match(/[a-zA-Z]/)));
how do i format a string of 2014-09-10 10:07:02 into something like this:
2014,09,10,10,07,02
Thanks!
Nice and simple.
var str = "2014-09-10 10:07:02";
var newstr = str.replace(/[ :-]/g, ',');
console.log(newstr);
Based on the assumption that you want to get rid of everything but the digits, an alternative is to inverse the regex to exclude everything but digits. This is, in effect, a white-listing approach as compared to the previously posted black-listing approach.
var dateTimeString = "2016-11-23 02:00:00";
var regex = /[^0-9]+/g; // Alternatively (credit zerkms): /\D+/g
var reformattedDateTimeString = dateTimeString.replace(regex, ',');
Note the + which has the effect of replacing groups of characters (e.g. two spaces would be replaced by only a single comma).
Also note that if you intend to use the strings as digits (e.g. via parseInt), numbers with a leading zero are interpreted within JavaScript as being base-8.
var string = "[Account0].&[1]+[Account1].&[2]+[Account2].&[3]+[Account3].&[4]";
var numbers = string.match(/(\d+)/gi);
alert(numbers.join(','));
here outpout is : 0,1,1,2,2,3,3,4
Here I need to elimanate all the strings with numbers and special characters.
can you please help me,is there any solution for the output : 1,2,3,4
Thanks,
Rajasekhar
The quick and easy solution would maybe be
var numbers = string.match(/\[\d+\]/gi); // numbers = ['[1]', '[2]', '[3]', '[4]'];
alert(numbers.join(',').replace(/[\[\]]/g, ''));
I have a page with some elements that are controlled by the user. One of these is a text input field, where the user is supposed to input a number. Everything works well if the user only inputs digits (EG 9000), but is the user uses comma notation (the being 9,000) javascript doesn't take the input as an integer.
How can I remove the commas and/or force the input to an integer? I tried using parseint(), but it doesn't seem to work with commas.
Use a global regular expression to replace all commas with an empty string:
var str = "12,345,678";
str = str.replace(/,/g, "");
parseInt(str, 10);
or even better
var s="jdjsghd0182.99";
var str = parseFloat(s.replace(/[^0-9 | ^.]/g, ''));
Or even better, given the general unreliability of user input, use this to get rid of all non-numeric characters:
var s = "9,Ljk876";
var t = parseInt(s.replace(/[^0-9]/g, ''));
alert ("s:" + s + ", t:" + t);
maybe
parseint(ny9000withCommas.replace(/\,/g,""))
lets talk about the restriction :
you can/should allow the user to enter both 9000 & 9,000
you can check validy via REGEX.
in the server side - you should eleminate the commas and treat it as integer.