I have a variable string like:
var myString = "857nano620348splitted3412674relation5305743";
How do I find the largest number from this?
I have tried like the below without any success.
var matches = myString.match(/d+/g);
I'd go for
var myString = "857nano620348splitted3412674relation5305743";
var largest = Math.max.apply(null, myString.match(/\d+/g));
FIDDLE
myString.match(/\d+/g) returns an array of the numbers, and using Math.max.apply(scope, array) returns the largest number in that array.
var numArray = xmr.match(/\d+/g); //this will store all numbers from xmr to numArray.
numArray.sort(function(a,b){return a-b});
var largest = numArray[numArray.length - 1];
You can use the following solution to find the largest number from a string using a regular expression:
var myString ="857nano620348splitted3412674relation5305743";
Math.max(...myString.match(/\d+/g))`
Related
I have this array and it is formatted as string:
['6.35', '2.72', '11.79', '183.25']
The problem is that when I convert it to numbers (using - without double quotes )
array.match(/\d+/g).map(Number) || 0;
it changes the dots used for decimals to commas. Then I end up with this new array:
6,35,2,72,11,79,183,25
So, instead of having 4 items inside the array, now I have 8 items, as my delimiters are commas.
Any ideas of how I can convert this array without replacing the dots?
Assuming you have an array in a string format, you can use the following regex to match all the decimals and then use .map(Number)
const str = "['6.35', '2.72', '11.79', '183.25']",
array = str.match(/\d+(?:\.\d+)?/g).map(Number)
console.log(array)
\d matches only digits, it's the short for [0-9]. For example, in 6.35 \d+ matches 6 and then 35 separately and the dot is ignored. What you get in result is array containing those matches.
As suggested in other answers, use of match is redundant in your case and you can go with:
array.map(Number)
You could just map numbers.
var array = ['6.35', '2.72', '11.79', '183.25'],
numbers = array.map(Number);
console.log(numbers);
var num = ['6.35', '2.72', '11.79', '183.25'].map(num => Number(num));
console.log(num);
Number() mdn
Parse the values to float :
console.log(['6.35', '2.72', '11.79', '183.25'].map(i => parseFloat(i)));
If for some reason .map() doesn't work just use a loop :
var array = ['6.35', '2.72', '11.79', '183.25']
var x = 0;
var len = array.length
while(x < len){
array[x] = parseFloat(array[x]);
x++
}
console.log(array)
Map over the array with the Number function, it will handle the conversion:
console.log(['6.35', '2.72', '11.79', '183.25'].map(Number));
If you want commas in your numbers, then you must stick with a string representation.
See this SO answer about a similar problem with ChartJS.
var arr = ["6,35,2,72,11,79,183,25"]
var result=arr.map(Number);
result[]
typeof(result[])
I was having the same problem this is a solution i found
i had
x = "11,1.1,100,100,2,3333,99"
and i wanted
x = [11,1.1,100,100,2,3333,99]
here's my solution
x.toString().replace(/, +/g, ",").split(",").map(Number)
I am trying to extract the numbers of this string: "ax(341);ay(20);az(3131);"
I think that I can do it how this:
var ax = data.split('(');
var ax2 = ax[1].split(')');
ax2[0] has "341"
Now If I can repeat this but starting in the next indexOf to take the second number.
I think that it's a bad practice, so I ask you If you have a better idea.
Thanks in advance
Use a regular expression:
var str = "ax(-341);ay(20);az(3131);"
var regex = /(-?\d+)/g
var match = str.match(regex);
console.log(match); // ["-341", "20", "3131"]
Now you can just access the numbers in the array as normal.
DEMO
You can use regex to extract all numbers from this.
var data = "ax(341);ay(20);az(3131);";
var ax = data.match(/\d+/g);
Here ax is now ["341", "20", "3131"]
Note that ax contains numbers as string. To convert them to number, use following
ax2 = ax.map( function(x){ return parseInt(x); } )
EDIT: You can alternatively use Number as function to map in the line above. It'll look like,
ax2 = ax.map( Number )
After this ax2 contains all the integers in the original string.
You could use a regular expression, eg:
var string = 'ax(341);ay(20);az(3131);';
var pattern = /([0-9]{1,})/g;
var result = string.match(pattern);
console.log(result);
// ["341", "20", "3131"]
http://regex101.com/r/zE9pS7/1
So I have objects listed like favoriteImage1, favoriteImage2... favoriteImage22. How do I get the number at the end of word? I tried parseInt but it returns undefined. Is there an easy way to do this?
Use a regular expression:
var string = "favoriteImage1";
var num = +string.replace(/\D/g, "");
If the name will always have the prefix "favoriteImage", you could also do
var x = "favoriteImage1";
var num = parseInt(x.substring(13));
I'd like to extract the numbers from the following string via javascript/jquery:
"ch2sl4"
problem is that the string could also look like this:
"ch10sl4"
or this
"ch2sl10"
I'd like to store the 2 numbers in 2 variables.
Is there any way to use match so it extracts the numbers before and after "sl"? Would match even be the correct function to do the extraction?
Thx
Yes, match is the way to go:
var matches = str.match(/(\d+)sl(\d+)/);
var number1 = Number(matches[1]);
var number2 = Number(matches[2]);
If the string is always going to look like this: "ch[num1]sl[num2]", you can easily get the numbers without a regex like so:
var numbers = str.substr(2).split('sl');
//chop off leading ch---/\ /\-- use sl to split the string into 2 parts.
In the case of "ch2sl4", numbers will look like this: ["2", "4"], coerce them to numbers like so: var num1 = +(numbers[0]), or numbers.map(function(a){ return +(a);}.
If the string parts are variable, this does it all in one fell swoop:
var str = 'ch2fsl4';
var numbers = str.match(/[0-9]+/g).map(function(n)
{//just coerce to numbers
return +(n);
});
console.log(numbers);//[2,4]
As an alternative just to show how things can be achieved in many different ways
var str = "ch2sl10";
var num1 = +(str.split("sl")[0].match(/\d+/));
var num2 = +(str.split("sl")[1].match(/\d+/));
Try below code
var tz = "GMT-7";
var tzOff = tz.replace( /[^+-\d.]/g, '');
alert(parseInt(tzOff));
i have:
var str="100px";
var number = str.split("px");
number = number[0];
var km = "100px";
var numberk = km.split("px");
numberk = numberk[0];
var gim = numberk+100;
var kim = number+100;
var fim = number+numberk;
document.write(gim+'<br>'+kim+'<br>'+jim+'<br>');
i would be thankfull if someone could me answere why the result are added like string rather than nummerical number in javascript i have used the isNaN(); function which shows this as a legal number. So how can this problem be solved.
thanks.
You could use the parseInt function in order to convert the string returned when spliting into integer:
number = parseInt(number[0], 10);
numberk = parseInt(numberk[0], 10);
Now the 2 variables are integers and you could perform integer operations on them.
You need to put parseInt() around each number before you use it. In fact, you could do this without removing the "px".
gim = parseInt(km) + 100;
simplest way to do this, you don't need to use split.
var str="150px";
var str1 = (parseInt(str)+100)+"px";
alert(str1);
OUTPUT:
200px
fiddle : http://jsfiddle.net/Kk3HK/1/
use parseInt()
var number = parseInt(str, 10);
var numberk = parseInt(km, 10);
Use parseInt to convert the string to a number.
var str = "100px";
var number = parseInt(str, 10);
parseInt stops when it finds the first non-number character, so you don't even need to remove the "px".
Wrap the numbers in parseInt().