parseInt not working, get the number from the string - javascript

I'm trying to get the number from a string, please see my below snippet, I try "parseInt" but unfortunately not working, any help, ideas please?
var tt = "test 12";
console.log(parseInt(tt));

parseInt() won't work since tt is not beginning with a number.
You can use match() with RegEx to extract number from a string.
var num = tt.match(/\d+/)[0];

Related

RegEx with any number replace by empty string

I am totally not confirm with Regular Expressions, so may you guys can help me out.
I have a String like "blablabla_300x300.jpg" where 300 can be any number
I need to replace the "_300x300" with ""
Can please someone provide me the correct answer (Javascript)
Thank you so much
Try this:
var s = "blablabla_300x300.jpg";
s = s.replace(/(_\d+x\d+)(\.jpg)$/, "$2");
console.log(s);

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

Replace all occurrences of character except in the beginning of string (Regex)

I'm trying to get rid of all minuses/dashes in a string number, except the first occurrence. After fiddling with Regex (JavaScript) for half an hour, still no results. Does anyone know the fix?
Given:
-123-45-6
Expected:
-123456
Given:
789-1-0
Expected:
78910
This one will do as well(it means dashes not at the beginning of the string):
(?!^)-
Example:
text = "-123-45-6".replace(/(?!^)-/g, "");
A simple solution :
s = s.replace(/(.)-/g,'$1')
Jutr try with:
'-123-45-6'.replace(/(\d)-/g, '$1');

javascript extract numbers from string not working

i have a sample str1 "14 girls"
str2 "178 guys"
i tried the following in chrome console to extract the numbers 12 and 178, could anyone please tell me what went wrong?
str.match(/^\d{2}$/) to get the number of girls i.e. `14`
str.match(/^[0-9]{2}?$/)
What would be an easy way to get the numbers?
If it's guaranteed that the numbers will always be at the start of the string, just use:
var n = parseInt(s, 10);
The parseInt() function will stop parsing at the first non-numeric character.
The reason your regular expressions didn't work is because you finished them with $ - meaning that they would only match if the entire string was a two digit number.
i think will be better:
str.match(/\d+(\.\d+)?/g)
This will give you an array of all numbers with float in it.
This is what I use:
var pattern=/[0-9]+/;
so...
var str='123abc';
//var str='abc123';
document.write(str.match(pattern));

remove values from a string

Does anyone know how I would remove all leading zeros from a string.
var str = 000890
The string value changes all the time so I need it to be able to remove all 0s before a number greater than 0. So in the example above it needs to remove the first three 0s. So the result would be 890
It looks like we each have our own ways of doing this. I've created a test on jsperf.com, but the results are showing
String(Number('000890'));
is the quickest (on google chrome).
Here are the numbers for the updated test based on #BenLee's comment for Firefox, IE, and Chrome.
See: this question
var resultString = str.replace(/^[0]+/g,"");
var resultString = str.replace(/^[0]+/g,"");
I think a function like this should work
function replacezeros(text){
var newText = text.replace(/^[0]+/g,"");
return newText;
}
If it needs to stay as a string, cast it to a number, and cast it back to a string:
var num = '000123';
num = String(Number(num));
console.log(num);
You could also use the shorthand num = ''+(+num);. Although, I find the first form to be more readable.
parseInt('00890', 10); // returns 890
// or
Number('00890'); // returns 890
If your problem really is as you defined it, then go with one of the regex-based answers others have posted.
If the problem is just that you have a zero-padded integer in your string and need to manipulate the integer value without the zero-padding, you can just convert it to an integer like this:
parseInt("000890", 10) # => 890
Note that the result here is the integer 890 not the string "890". Also note that the radix 10 is required here because the string starts with a zero.
return str.replace(/^0+(.)/, '$1'));
That is: replace maximum number of leading zeros followed by any single character (which won't be a zero), with that single character. This is necessary so as not to swallow up a single "0"
you can simply do that removing the quotation marks.
var str = 000890;
//890
var str = "000890";
//000890

Categories