I'm trying to execute following code :
var pwd = "CURRENT DATE - 23 DAYS";
var Exp = /^([CURRENT]{7}[ ]{1}[DATE]{4}[ ]{1}[-]{1}[ ]{1}[\d]+[ ]{1}[DAYS]{4})$/i;
var Exp2 = /^([CURRENT]{7}[ ]{1}[DATE]{4}[ ]{1}[-]{1}[ ]{1}[\d]+[ ]{1}[MONTHS]{6})$/i;
if (pwd.match(Exp) || pwd.match(Exp2)) {
alert("SUCCESS");
} else {
alert("ERROR");
}
It works as expected... But though I have given first-word CURRENT and mentioned 7 characters, It accepts if I give the wrong word like CUUUENT instead of CURRENT. How can I resolve this issue?
The sub-expression [CURRENT]{7} is saying repeat any of the letters in the set [CURRENT] seven times. What I think you meant to intend was to match the string CURRENT. That would just be CURRENT.
Much of your regular expressions are specifying the same thing. /^CURRENT DATE - [0-9]+ (DAYS|MONTHS)$/ should match what you have well enough.
With [CURRENT]{7} you match every character from [], and 7 of them. If you want to match the whole word, use (CURRENT)
It'd be better with (CURRENT DATE - \d{1} DAYS)
Note that whn you writr the whole word, it matches kt exactly as it it written. But [THIS]{4} captures any combination of 4 letters between [] like ITHS or HHHT.
Related
This question already has an answer here:
Find all regex matches
(1 answer)
Closed last year.
Okay, so I have this string "nesˈo:tkʰo:x", and I want to get the index of all the zero-width positions that don't occur after any instance of the character ˈ (the IPA primary stress symbol). So in this case, those expected output would be 0, 1, 2, and 3 - the indices of the letters nes that occur before the one and only instance of ˈ, plus the ˈ itself.
I'm doing this with regex for reasons I'll get into in a bit. Regex101 confirms that /(?=.*?ˈ)/ should match all 4 of those zero-width positions with JS' regex flavor... but I can't actually get JS to return them.
A simple setup might look like this:
let teststring = "nesˈo:tkʰo:x";
let re = new RegExp("(?=.*?ˈ)", "g");
while (result = re.exec(teststring)) {
console.log("Match found at "+result.index);
}
...except that this loops forever. It seems to get stuck on the first match, which I understand has something to do with how RegExp.exec is supposed to auto-increment RegExp.lastIndex for global regexes, or something. But I also can't make the regex not global, or it won't return all the matches for strings like this where more than one match is expected.
Okay, so what if I manually increment RegExp.lastIndex to prevent it from looping?
let teststring = "nesˈo:tkʰo:x";
let re = new RegExp("(?=.*?ˈ)", "g");
while (result = re.exec(teststring)) {
if (result.index == re.lastIndex) {
re.lastIndex++;
} else {
console.log("Match found at "+result.index);
}
}
Now it... prints out nothing at all. Now, to be fair, if lastIndex starts at 0 by default, and the index of the first match is 0, I half expect that to be skipped over... but why isn't it at least giving me 1, 2 and 3 as matches?
Now, I can already hear the chorus of "you don't need regex for this, just do Array(teststring.indexOf("ˈ")).keys() or something to generate [0,1,2,3]". That may work for this specific example, but the actual use case is a parser function that's supposed to be a general solution for "for this input string, replace all instances of A with B, if condition C is true, unless condition D is true". Those conditions might be something like "if A is at the end of the string" or "if A is right next to another instance of A" or "if A is between 'n' and 't'". That kind of complicated string matching problem is why the parser creates and executes regexes on the fly and why regex is getting involved, and it does work for almost everything except this one annoying edge case, which I'd rather not have to refactor the entire mechanism of the parser to deal with if I don't have to.
Use String.prototype.matchAll() to get all the matches.
let teststring = "nesˈo:tkʰo:x";
let re = new RegExp("(?=.*?ˈ)", "g");
[...teststring.matchAll(re)].forEach(result =>
console.log("Match found at " + result.index)
)
.search() returns the index of a match. .exec() returns an array of the match. Note a look ahead (?=) isn't needed, a standard capture group () suffices.
const str =`nesˈo:tkʰo:x",`;
const rgx = /(.*?ˈ)/;
let first = str.search(rgx);
let last = rgx.exec(str)[0].length - 1;
console.log('Indices: '+first+' - '+(first + last)+' \nLength: '+(last+1));
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));
I have a string with times (formatted HH:MM) each on a new line. I want to create a JS function to check if there is any times that does not belong. It should simply return true or false.
Example correct string: var s = "5:45\n07:00\n13:00\n17:00";
5:45
07:00
13:00
17:00
Example incorrect string: var s = "5:45\n07:00\n55:00\n17:00";
5:45
07:00
55:00 // incorrect date here, should return false
17:00
My regex experience is little to none. Playing around on Scriptular I created this expression to detect times that do match:
/^[0-2]?[0-9]\:[0-5][0-9]$/m. This however is not sufficient.
So, how can I get this to work with a string s as indicated above?
function checkIfStringConforms(s)
{
var all_good = [some magic with regex here]
return all_good;
}
PS: I have Googled around and checked answers on SO. My regex skill is... eh.
Your regex is OK, but it would also match 29:00, so it needs some improvement. Then, it's always a bit more difficult to find non-matches than it is to find matches. You could try and remove all matches from the string and then see if it's empty (except for whitespace):
result = s.replace(/^(?:2[0-3]|[01]?[0-9]):[0-5][0-9]$/mg, "");
If result is empty after that, there were no illegal times in your string.
It can be done without the use of any regex. Just split on new-line and see if every date matches your format. For that we could use Array.every
function checkIfStringConforms(s) {
return s.split("\n").every(function(str){
var arr = str.split(":");
return (arr[0] < 24 && arr[0] > -1) && arr[1] < (60 && arr[1] > -1)
});
}
/(((2[^0-3]|[3-9].):..)|(..?:[^0-5].))(\n|$)/
Regexp returns true if your s var has at least one invalid time. Please, check it carefully before use – your question is quite broad and restrictions are not fully defined. Regex assumes that you have something like x:xx or xx:xx in each line (x is a digit) – I’m not sure this assumption covers all your data.
I have the following example url: #/reports/12/expense/11.
I need to get the id just after the reports -> 12. What I am asking here is the most suitable way to do this. I can search for reports in the url and get the content just after that ... but what if in some moment I decide to change the url, I will have to change my algorythm.
What do You think is the best way here. Some code examples will be also very helpfull.
It's hard to write code that is future-proof since it's hard to predict the crazy things we might do in the future!
However, if we assume that the id will always be the string of consecutive digits in the URL then you could simply look for that:
function getReportId(url) {
var match = url.match(/\d+/);
return (match) ? Number(match[0]) : null;
}
getReportId('#/reports/12/expense/11'); // => 12
getReportId('/some/new/url/report/12'); // => 12
You should use a regular expression to find the number inside the string. Passing the regular expression to the string's .match() method will return an array containing the matches based on the regular expression. In this case, the item of the returned array that you're interested in will be at the index of 1, assuming that the number will always be after reports/:
var text = "#/reports/12/expense/11";
var id = text.match(/reports\/(\d+)/);
alert(id[1]);
\d+ here means that you're looking for at least one number followed by zero to an infinite amount of numbers.
var text = "#/reports/12/expense/11";
var id = text.match("#/[a-zA-Z]*/([0-9]*)/[a-zA-Z]*/")
console.log(id[1])
Regex explanation:
#/ matches the characters #/ literally
[a-zA-Z]* - matches a word
/ matches the character / literally
1st Capturing group - ([0-9]*) - this matches a number.
[a-zA-Z]* - matches a word
/ matches the character / literally
Regular expressions can be tricky (add expensive). So usually if you can efficiently do the same thing without them you should. Looking at your URL format you would probably want to put at least a few constraints on it otherwise the problem will be very complex. For instance, you probably want to assume the value will always appear directly after the key so in your sample report=12 and expense=11, but report and expense could be switched (ex. expense/11/report/12) and you would get the same result.
I would just use string split:
var parts = url.split("/");
for(var i = 0; i < parts.length; i++) {
if(parts[i] === "report"){
this.reportValue = parts[i+1];
i+=2;
}
if(parts[i] === "expense"){
this.expenseValue = parts[i+1];
i+=2;
}
}
So this way your key/value parts can appear anywhere in the array
Note: you will also want to check that i+1 is in the range of the parts array. But that would just make this sample code ugly and it is pretty easy to add in. Depending on what values you are expecting (or not expecting) you might also want to check that values are numbers using isNaN
I have a regex problem with validation for a region code.
My region code could be only one digit but it also could be a digits separated by '-'
for Example my region code could be one of the following:
6
6-66
77-7
As you can see I must have at least one digit or digits separated by '-' and if they are separated there should be a digits after the '-' sign (does not matter how many). So 6- must not be validated as legal region code. I try 2 hours to solve this, but I couldn't, so please help me! Thank you!
/\d+(-\d+)?$/
This will match 6, 6-66,77-7, but not6-`
If what you are looking for is the whole string:
/^\d+(?:-\d+)?$/
or something like that:
if (parseInt(yourstring.split(/-/)[0])>=eval(yourstring)) alert('true');
else alert('false');
But it is more complicated :) and less efficient! And if the condition is false you code will crash!
var data = ['6', '6-66', '77-7', '6-'];
var len = data.length;
for(var i=0; i<len; ++i) {
var current = data[i];
var result = data[i].match(/^(\d+|\d+[-]\d+)$/);
if(result != null) {
console.log(current);
}
}
--output:--
6
6-66
77-7
For a quick answer you can try following:
/^([0-9])|([0-9]\-[0-9][0-9])|([0-9][0-9]\-[0-9])$/
or in case your engine support perl-styled character classes:
/^(\d)|(\d\-\d\d)|(\d\d\-\d)$/
here what it does:
between / and / resides as string defining a regular expression
\d stands for one digit it coudl also be writen as [0-9]
() defines a sub-expression, so (\d) matches your first one-digit, (\d-\d\d) second three digits style, and last (\d\d-\d) third variant of three-digit region code
| goes as "OR" like (A)|(B)|(C), so by combining previous three we will get:
/(\d)|(\d-\d\d)|(\d\d-\d)/
Finally ^ means start of string, and $ - end of string.
also there is so called BRE mode (in which you have to add "\" symbol before each parentheses), but I think it is not the case. However if you would have some free time, please consider any quick tutorial like this one.