Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have the following string:
12.0024.0024.0024.0036.0012.0012.0012.004.006.008.004.004.0012.0012.0012.00
I want to create an array that is split after 2 decimal place, that should look something like this, if I were to instantiate it:
var numArray = new Array ('12.00', '24.00', '24.00', '24.00', '36.00', '12.00', '12.00', '12.00', '4.00', '6.00', '8.00', '4.00', '4.00', '12.00', '12.00', '12.00')
You might be able to split the input string at the interface between a non zero digit on the left and a zero digit on the right:
var input = "12.0024.0024.0024.0036.0012.0012.0012.004.006.008.004.004.0012.0012.0012.00";
var nums = input.split(/(?<=0)(?=[^\D0])/);
console.log(nums);
But, this only coincidentally works here because every float number in the string happens to end with trailing zeroes. If that were not the case, the above method would fail. A better approach would be to manage your data better and not work with such cryptic number strings.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
const a = 043
console.log(Number(a))
Here the variable a is octal because of which we get the result as 35.
Instead, I want the variable a to be a number 43.
Found results for removing leading zeros from a string(Remove leading zeros from a number in Javascript)
Because your value starts with 0, and its type is number instead of string, it will be recognized as octal, and when you use it directly, javascript will convert it to decimal
The fastest way you can use Number.prototype.toString(radix)
let a = 043
console.log(a.toString(8))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to ensure the language is always extracted from a string, only before the 2nd dash (-)
So
en-AU-Option-A
becomes
en-AU
Is this possible with a single line of Javascript?
Split takes a second argument to specify no. of element desired. so in this case you can specify 2 and it will split upto 2nd - and than join -
let str = `en-AU-Option-A`
console.log(str.split('-',2).join('-'))
Of course it is possible. This is not using regex.
let mystr = 'en-AU-Option-A'
console.log(mystr.split('-').slice(0, 2).join('-'))
Here is a regex solution
var regex = /^([a-z]+|[A-Z]+)\-([a-z]+|[A-Z]+)/g;
var str = 'en-AU-Option-A';
console.log(str.match(regex)) // ["en-AU"]
Try This:
var patt = /[^-]*-[^-]*/ig ;
console.log( patt.exec( 'en-AU-Option-A' )[0] )
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to remove + or 00 from the beginning of a number in case they exist. So a number like +37253783478 would output 37253783478 and 0037253783478 would output 37253783478. What would the regex look like that matches this pattern?
EDIT: I've managed to remove the leading zeros using ^0+ but I can't figure out how to match both cases.
If I understand the requirement, the following will match both cases. Essentially, what you need to do is use the regex or operator |.
The following will remove all leading 0s
str.replace(/(^0+|^\+)/,'')
But if you just need to remove exactly two leading 0s, use this:
str.replace(/(^00|^\+)/,'')
And here it is in action on your examples:
let nums = ['+37253783478', '0037253783478', '0037253780478', '375378+0478'];
let replaced = nums.map(num => num.replace(/(^0+|^\+)/,''));
console.log(replaced);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string like
count-contribute-1
count-contribute-11
count-contribute-1111
Here I want to split the string and get the last split value (i.e 1 , 11, 1111);
How can I do it?
split() on - and pop() of the last value
string.split('-').pop()
Use .pop() to get the last item from the array created by .split()
"count-contribute-1".split('-').pop();
Also you can get last part of numbers using regular expression. Like this:
s = "count-contribute-111"
s.match(/\d+$/)
//return "111"
It doesn't matter what separator you use.
s = "count-contribute-+*%111"
s.match(/\d+$/)
//return "111"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
here is my regular expression
/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/
There will be one condition: the length of every string after first 40 characters must be 16 characters. This 16 characters string will be non repeating and minimum of 2 times and maximum of 10 times. So i want to get the length of this sub-string which should be 16.
Here is input string:
string input="PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522"
I want to match the length of "GEX2421262506027" and "GEX2437345435522". Like this, there will be 10 strings max. I want to validate if this length should be 16 characters.
try this
var pattern = /^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/;
var exp = text.match(pattern);
if (exp) {
alert(exp[0].length);
}
if you just want the last 16 characters:
var string = 'PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522';
var matching = string.match(/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/);
console.log(matching[1]);
console.log(matching[1].length);