How to replace a decimal in a number with a string? - javascript

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.

Related

Extract number and text from string with RegExp exec Javascript

I'm trying to extract a number and text from strings like those: 171Toberin, [171]Toberin or [171] Toberin.
I have this RegExp /(?<code>\d+)(?<name>\w+)/u, and this RegExp only works with 171Toberin.
You can use the below regex to remove all non alphanumeric characters.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.replace(/[^a-zA-Z0-9]/g, ''));
Or use the below to extract the alphanumeric characters from string.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.match(/[a-zA-Z0-9]+/g));
Or if you want to extract numbers and strings in separate array then use the below one.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.match(/[0-9]+/g));
console.log(string.match(/[a-zA-Z]+/g));
Please try with this: (?<code>\d+)[^\d\w]*(?<name>\w+)/ug
It works with the entire sentence: 171Toberin, [171]Toberin or [171] Toberin.
Returning 3 matches. You can try it at https://regex101.com/
With [^\d\w]* you omit possible numbers and words in between. With g flag you return all matches.

How to remove the alphabet in my case using JQuery?

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

Javascript timestamp formatting with regular expression?

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.

extract decimal numbers from string in ajax

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

Using JavaScript's parseInt at end of string

I know that
parseInt(myString, 10) // "Never forget the radix"
will return a number if the first characters in the string are numerical, but how can I do this in JavaScript if I have a string like "column5" and want to increment it to the next one ("column6")?
The number of digits at the end of the string is variable.
parseInt("column5".slice(-1), 10);
You can use -1 or -2 for one to two digit numbers, respectively.
If you want to specify any length, you can use the following to return the digits:
parseInt("column6445".match(/(\d+)$/)[0], 10);
The above will work for any length of numbers, as long as the string ends with one or more numbers
Split the number from the text, parse it, increment it, and then re-concatenate it. If the preceding string is well-known, e.g., "column", you can do something like this:
var precedingString = myString.substr(0, 6); // 6 is length of "column"
var numericString = myString.substr(7);
var number = parseInt(numericString);
number++;
return precedingString + number;
Try this:
var match = myString.match(/^([a-zA-Z]+)([0-9]+)$/);
if ( match ) {
return match[1] + (parseInt(match[2]) + 1, 10);
}
this will convert strings like text10 to text11, TxT1 to Txt2, etc. Works with long numbers at the end.
Added the radix to the parseInt call since the default parseInt value is too magic to be trusted.
See here for details:
http://www.w3schools.com/jsref/jsref_parseInt.asp
basically it will convert something like text010 to text9 which is not good ;).
var my_car="Ferrari";
var the_length=my_car.length;
var last_char=my_car.charAt(the_length-1);
alert('The last character is '+last_char+'.');
Credit to http://www.pageresource.com/jscript/jstring1.htm
Then just increment last_char
Split the word and number using RegEx.
using parseInt() increment the number.
Append to the word.
Just try to read string char by char, checking its ASCII code. If its from 48 to 57 you got your number. Try with charCodeAt function. Then just split string, increment the number and its done.

Categories