Regex replace for preceding zero twice in string with different condition - javascript

I have string XY4PQ43 and I want regex replace should output XY04PQ0043 in JavaScript. For first number in a string I want zero prefix if its single digit to make it 2 digits and for second number in string I want prefix zeros if its less than 4 digit number. Below output expected for regex.
AB1CD123 => AB01CD0123
MN12XYZ1 => MN12XY0001
LJ99P1234 => IJ99P1234
Any jsfiddle or codepen example preferred

Try this.
function format(text) {
let match = /^(.*?)(\d+)(.*?)(\d+)$/.exec(text);
return [
match[1],
match[2].padStart(2, '0'),
match[3],
match[4].padStart(4, '0'),
].join('');
}
console.log(format('AB1CD123'));
console.log(format('MN12XYZ1'));
console.log(format('LJ99P1234'));

For that given string, you can apply the following regex:
var _str = 'AB1CD123';
_str.match(new RegExp(/([A-Z]{2})([0-9]{1,2})([A-Z]{2})([0-9]{1,4})/))
It outputs an array with values matched starting from 1 to 4, where 2 and 4 are the ones you need to manage. For those values you can apply logic - add leading zeros - by checking their length and merge them afterwards. Try it in browser console.
Note: it works for this specific example. For other examples you need to adjust the length matched.

Related

Select 4 last digits in any string

I'm having an array of various string and I need to select from each of the strings last 4 digits that are not separated.
var example = ["eg3322-3748", "eg3322-3749_ABCD", "eg3750_5GHJ"];
The desired result should give these results:
var example = ["3748", "3749", "3750"]
Thanks! :)
This should do it:
const input = ['eg3322-3748', 'eg3322-3749_ABCD', 'eg3750_5GHJ'];
const result = input.map(item =>
item
.split('').reverse().join('') // Reverse string
.match(/\d{4}/)[0] // Find first 4 consecutive digits
.split('').reverse().join('') // Reverse matches back
);
console.log(result);
PS: next time you ask a question, please show what you attempted. Thanks!
Edit: this will generate an error if any string does not contain 4 consecutive digits. You can easily fix that by adding a fallback to match (with [''], for instance, then it'll return that).
Logic:
Based on given data, its obvious, you are looking for a batch of alphanumeric/numeric characters and wish to get last 4 digits.
So you can create a regex that matches all such groups, and returns last one.
Then, from this last group, just extract last 4 characters and Ta-da!!!
var example = ["eg3322-3748", "eg3322-3749_ABCD", "eg3750_5GHJ"];
var regex = /([a-z]*\d{4,})/g;
var output = example.map((str) => {
const match = str.match(regex).pop();
return match.substr(-4);
});
console.log(output)
You can use .substr() function to slice the length of string. Maybe "example(i).substr(example(i).length - 4)" will work.

Applying currency format using replace and a regular expression

I am trying to understand some code where a number is converted to a currency format. Thus, if you have 16.9 it converts to $16.90. The problem with the code is if you have an amount over $1,000, it just returns $1, an amount over $2,000 returns $2, etc. Amounts in the hundreds show up fine.
Here is the function:
var _formatCurrency = function(amount) {
return "$" + parseFloat(amount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')
};
(The reason the semicolon is after the bracket is because this function is in itself a statement in another function. That function is not relevant to this discussion.)
I found out that the person who originally put the code in there found it somewhere but didn't fully understand it and didn't test this particular scenario. I myself have not dealt much with regular expressions. I am not only trying to fix it, but to understand how it is working as it is now.
Here's what I've found out. The code between the backslash after the open parenthesis and the backslash before the g is the pattern. The g means global search. The \d means digit, and the (?=\d{3})+\. appears to mean find 3 digits plus a decimal point. I'm not sure I have that right, though, because if that was correct shouldn't it ignore numbers like 5.4? That works fine. Also, I'm not sure what the '$1,' is for. It looks to me like it is supposed to be placed where the digits are, but wouldn't that change all the numbers to $1? Also, why is there a comma after the 1?
Regarding your comment
I was hoping to just edit the regex so it would work properly.
The regex you are currently using is obviously not working for you so I think you should consider alternatives even if they are not too similar, and
Trying to keep the code change as small as possible
Understandable but sometimes it is better to use a code that is a little bit bigger and MORE READABLE than to go with compact and hieroglyphical.
Back to business:
I'm assuming you are getting a string as an argument and this string is composed only of digits and may or may not have a dot before the last 1 or 2 digts. Something like
//input //intended output
1 $1.00
20 $20.00
34.2 $34.20
23.1 $23.10
62516.16 $62,516.16
15.26 $15.26
4654656 $4,654,656.00
0.3 $0.30
I will let you do a pre-check of (assumed) non-valids like 1. | 2.2. | .6 | 4.8.1 | 4.856 | etc.
Proposed solution:
var _formatCurrency = function(amount) {
amount = "$" + amount.replace(/(\d)(?=(\d{3})+(\.(\d){0,2})*$)/g, '$1,');
if(amount.indexOf('.') === -1)
return amount + '.00';
var decimals = amount.split('.')[1];
return decimals.length < 2 ? amount + '0' : amount;
};
Regex break down:
(\d): Matches one digit. Parentheses group things for referencing when needed.
(?=(\d{3})+(\.(\d){0,2})*$). Now this guy. From end to beginning:
$: Matches the end of the string. This is what allows you to match from the end instead of the beginning which is very handy for adding the commas.
(\.(\d){0,2})*: This part processes the dot and decimals. The \. matches the dot. (\d){0,2} matches 0, 1 or 2 digits (the decimals). The * implies that this whole group can be empty.
?=(\d{3})+: \d{3} matches 3 digits exactly. + means at least one occurrence. Finally ?= matches a group after the main expression without including it in the result. In this case it takes three digits at a time (from the end remember?) and leaves them out of the result for when replacing.
g: Match and replace globally, the whole string.
Replacing with $1,: This is how captured groups are referenced for replacing, in this case the wanted group is number 1. Since the pattern will match every digit in the position 3n+1 (starting from the end or the dot) and catch it in the group number 1 ((\d)), then replacing that catch with $1, will effectively add a comma after each capture.
Try it and please feedback.
Also if you haven't already you should (and SO has not provided me with a format to stress this enough) really really look into this site as suggested by Taplar
The pattern is invalid, and your understanding of the function is incorrect. This function formats a number in a standard US currency, and here is how it works:
The parseFloat() function converts a string value to a decimal number.
The toFixed(2) function rounds the decimal number to 2 digits after the decimal point.
The replace() function is used here to add the thousands spearators (i.e. a comma after every 3 digits). The pattern is incorrect, so here is a suggested fix /(\d)(?=(\d{3})+\.)/g and this is how it works:
The (\d) captures a digit.
The (?=(\d{3})+\.) is called a look-ahead and it ensures that the captured digit above has one set of 3 digits (\d{3}) or more + followed by the decimal point \. after it followed by a decimal point.
The g flag/modifier is to apply the pattern globally, that is on the entire amount.
The replacement $1, replaces the pattern with the first captured group $1, which is in our case the digit (\d) (so technically replacing the digit with itself to make sure we don't lose the digit in the replacement) followed by a comma ,. So like I said, this is just to add the thousands separator.
Here are some tests with the suggested fix. Note that it works fine with numbers and strings:
var _formatCurrency = function(amount) {
return "$" + parseFloat(amount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
};
console.log(_formatCurrency('1'));
console.log(_formatCurrency('100'));
console.log(_formatCurrency('1000'));
console.log(_formatCurrency('1000000.559'));
console.log(_formatCurrency('10000000000.559'));
console.log(_formatCurrency(1));
console.log(_formatCurrency(100));
console.log(_formatCurrency(1000));
console.log(_formatCurrency(1000000.559));
console.log(_formatCurrency(10000000000.559));
Okay, I want to apologize to everyone who answered. I did some further tracing and found out the JSON call which was bringing in the amount did in fact have a comma in it, so it is just parsing that first digit. I was looking in the wrong place in the code when I thought there was no comma in there already. I do appreciate everyone's input and hope you won't think too bad of me for not catching that before this whole exercise. If nothing else, at least I now know how that regex operates so I can make use of it in the future. Now I just have to go about removing that comma.
Have a great day!
Assuming that you are working with USD only, then this should work for you as an alternative to Regular Expressions. I have also included a few tests to verify that it is working properly.
var test1 = '16.9';
var test2 = '2000.5';
var test3 = '300000.23';
var test4 = '3000000.23';
function stringToUSD(inputString) {
const splitValues = inputString.split('.');
const wholeNumber = splitValues[0].split('')
.map(val => parseInt(val))
.reverse()
.map((val, idx, arr) => idx !== 0 && (idx + 1) % 3 === 0 && arr[idx + 1] !== undefined ? `,${val}` : val)
.reverse()
.join('');
return parseFloat(`${wholeNumber}.${splitValues[1]}`).toFixed(2);
}
console.log(stringToUSD(test1));
console.log(stringToUSD(test2));
console.log(stringToUSD(test3));
console.log(stringToUSD(test4));

Regular expression for remove everything except 10 number?

I learn right now regular expression and need to know, how I can remove all except 10 numbers or maximum 10 number, I tried to create RegExp like this
var value = value.replace(/[^\d]/g, '')
You can use regex's {0,10} range of times to specify the length of the number.
My example will produce to matches,
[
"1348737734",
"8775"
]
It will match first number with the length of 10, and the rest of the number.
const str = 'asb13487377348775nvnn';
const result = str.match(/(\d{1,10})/g);
console.log(result);

Javascript regex match returning a string with comma at the end

Just as the title says...i'm trying to parse a string for example
2x + 3y
and i'm trying to get only the coefficients (i.e. 2 and 3)
I first tokenized it with space character as delimiter giving me "2x" "+" "3y"
then i parsed it again to this statement to get only the coefficients
var number = eqTokens[i].match(/(\-)?\d+/);
I tried printing the output but it gave me "2,"
why is it printing like this and how do i fix it? i tried using:
number = number.replace(/[,]/, "");
but this just gives me an error that number.replace is not a function
What's wrong with this?
> "2x + 3y".match(/-?\d+(?=[A-Za-z]+)/g)
[ '2', '3' ]
The above regex would match the numbers only if it's followed by one or more alphabets.
Match is going to return an array of every match. Since you put the optional negative in a parentheses, it's another capture group. That capture group has one term and it's optional, so it'll return an empty match in addition to your actual match.
Input 2x -> Your output: [2,undefined] which prints out as "2,"
Input -2x -> Your output: [2,-]
Remove the parentheses around the negative.
This is just for the sake of explaining why your case is breaking but personally I'd use Avinash's answer.

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