I have used Globalize.js to localize and format the date. it all works fine in different culture, but not working properly in German culture (de-DE). Code i have used to format.
Globalize.format(new Date(), "MM/yy/dd","de-DE");
it returns "10.14.01". i expecting the value as "10/14/01".
what might be the problem. is that issue in globalize? please anyone help me to come out of this headbang.
finally i found the cause of the problem. In globalize.culture.de-DE culture file
calendars: {
standard: {
"/": ".",
firstDay: 1,
....
.....
}
some standard has been handled like above. could any help me about why this code block has been used?
The culture de-De is German, use nl-NL instead.
It seems that you are using the old version of Globalize.js, which works rather well but isn’t developed any more, and it can be difficult to find documentation of it except in my book.
The rules for the format argument are somewhat obscure, but when a format like "MM/yy/dd" does not work, put any characters that should appear “as is” inside Ascii apostrophes, in this case
"MM'/'yy'/'dd"
Some punctuation characters can be used inside the format string without such quoting, but when in doubt, quote.
Related
I'm trying to use a powershell regex to pull version data from the AssemblyInfo.cs file. The regex below is my best attempt, however it only pulls the string [assembly: AssemblyVersion(". I've put this regex into a couple web regex testers and it LOOKS like it's doing what I want, however this is my first crack at using a powershell regex so I could be looking at it wrong.
$s = '[assembly: AssemblyVersion("1.0.0.0")]'
$prog = [regex]::match($s, '([^"]+)"').Groups[1].Value
You also need to include the starting double quotes otherwise it would start capturing from the start until the first " is reached.
$prog = [regex]::match($s, '"([^"]+)"').Groups[1].Value
^
Try this regex "([^"]+)"
Regex101 Demo
Regular expressions can get hard to read, so best practice is to make them as simple as they can be while still solving all possible cases you might see. You are trying to retrieve the only numerical sequence in the entire string, so we should look for that and bypass using groups.
$s = '[assembly: AssemblyVersion("1.0.0.0")]'
$prog = [regex]::match($s, '[\d\.]+').Value
$prog
1.0.0.0
For the generic solution of data between double quotes, the other answers are great. If I were parsing AssemblyInfo.cs for the version string however, I would be more explicit.
$versionString = [regex]::match($s, 'AssemblyVersion.*([0-9].[0-9].[0-9].[0-9])').Groups[1].Value
$version = [version]$versionString
$versionString
1.0.0.0
$version
Major Minor Build Revision
----- ----- ----- --------
1 0 0 0
Update/Edit:
Related to parsing the version (again, if this is not a generic question about parsing text between double quotes) is that I would not actually have a version in the format of M.m.b.r in my file because I have always found that Major.minor are enough, and by using a format like 1.2.* gives you some extra information without any effort.
See Compile date and time and Can I generate the compile date in my C# code to determine the expiry for a demo version?.
When using a * for the third and fourth part of the assembly version, then these two parts are set automatically at compile time to the following values:
third part is the number of days since 2000-01-01
fourth part is the number of seconds since midnight divided by two (although some MSDN pages say it is a random number)
Something to think about I guess in the larger picture of versions, requiring 1.2.*, allowing 1.2, or 1.2.3, or only accepting 1.2.3.4, etc.
I am making a schedule application (JS) that needs to both validate an input, and get the various parts of the input. Some sample valid inputs:
8a12p(8am to 12pm)
6a6p (6am to 6pm)
My regex string is currently ^[1-9]|1[0-2][a|p][1-9]|1[0-2][a|p]$, but it doesn't seem to work for all cases (e.g. 8a12x still is valid), and the parts don't seem to split properly in JS. (I need to be able to get the various parts (e.g. [ "8", "a", "12", "p" ] for valid regex.)
Thanks!
Your problem is that you are not using the correct parenthesis.
This works: ^([1-9]|1[0-2])(a|p)([1-9]|1[0-2])(a|p)$
Recomended site for building regex's https://regex101.com/
Explanation on yours
Your regex ^[1-9]|1[0-2][a|p][1-9]|1[0-2][a|p]$ looks for either
^[1-9] or 1[0-2][a|p][1-9] or [1|p]$ and since the first works with your example (since it starts with a number between 1 and 9 the whole regex is true.
I feel like I'm pretty close to the solution here, but I can't quite seem to figure it out. My goal is to take the set of strings one at a time, [ 'en', 'en-us', 'en_us', 'zh-hans-TW' ] and produce [ 'en', 'en', 'en', 'zh-hans' ]. I've tried a few different things, but don't have quite the right solution.
This is the closest I've come, I believe, matching all but 'en'.
/([a-zA-Z-_]+)[-_].+/
(One or more of aA-zZ chars or -_ followed by - or _ and additional chars)
I tried negative lookahead (which I'm not real good at), and came up with this which over matches and captures the whole string
/([a-zA-Z-_]+)(?![-_].+)/
(One or more aA-zZ chars or -_ not followed by - or _ with additional characters)
Could someone point out the right solution here?
Instead of matching the portions of the strings you wish to keep, you could remove the ends of the strings you do not want to keep:
/[-_][a-z]+$/i
Here is an implementation in Javascript:
var array1 = [ 'en', 'en-us', 'en_us', 'zh-hans-TW' ];
var array2 = array1.map(function(str) {
return str.replace(/[-_][a-z]+$/i, "");
});
console.log(array2);
This outputs:
[ 'en', 'en', 'en', 'zh-hans' ]
You should try to be more general. For instance, de-DE-u-co-phonebk is also a valid language code (the stuff starting with -u... represents Unicode options for collation order etc.). I'm assuming you want to strip off everything starting from the country code, which by the standard is supposed to be uppercase. If you want to do this with a regexp, then
function strip_country_code(lang) { return lang.replace(/[-_][A-Z][A-Z].*$/, ''); }
Of course, this will fail on en-us, which is invalid; it should be en-US. You have to decide if and how to handle invalid language codes such as this.
That's just one reason that you would be better off using available libraries to process language codes if possible. Take a look at the JS internationalization API, which has several ways to parse locale codes and find the "best" one. However, browser support is limited. So you might want to look for something off the shelf. But I can't put my finger on anything at the moment.
The JED library uses the following regexp to extract segments:
str.match(/[a-z]+/gi)
but then assumes that the second segment if present is always the country, so this logic would fail on zh-hans-TW.
You should also consider who is going to be consuming the result of your string manipulation. Are you saying that there is some library, or API, that can only handle the part of the locale string preceding the country code? You should ensure that that is in fact the case. For instance, I believe that moment.js also will handle different locale strings properly.
I am using a JS date library which has a simple asString() formatting syntax e.g. dd mmm yyyy produces 01 Jan 1970.
Unfortunately should the month happen to contain a letter that appears in the formatting string it can go wrong, e.g. `Date('2014-09-01').asString('dd mmm yyyy') = 01 Septe9ber 2014'
To solve this is quite simple; alter the asString() method to use the format '[dd] [mmm] [yyyy]' instead. However this comes from a global format string used by other methods. The only method that needs the square brackets is the asString method.
So my ideal solution is to simply add a function in that method which replaces any of the following strings within the format string:
formats=['yyyy','yy','mmmm','mmm','mm','m','dddd','ddd','dd','d','hh','min','ss'];
With itself surrounded by []
dd/mm/yyyy => [dd]/[mm]/[yyyy]
Unfortunately the RegEx is proving to be complex - simply looping through each item results in [[d][d]]/[[m][m]]/[[yy][yy]].
So I'd like help writing this RegEx. If it can't be done please say so - I'm not interested in using new libraries as a solution but would consider solutions which solved the problem in a different way within the current asString method (i.e. no breaking changes)
This should do:
var regex = /(min|y+|m+|d+|h+|s+)/g,
newString = format.replace(regex,'[$1]');
Tested with the format "dd/mm/yyyy", resulted in "[dd]/[mm]/[yyyy]"
I'm using the last build of date-fr-FR.js in the svn trunk (rev 191).
The parsing seems to fail on days and months names.
Date.parse("9 3 2012")
is ok, but:
Date.parse("vendredi 9 mars 2012")
returns null.
parseExact doesn't help either:
Date.parseExact("vendredi 9 mars 2012", "dddd d MMMM yyyy")
returns null.
Anyone faced a similar issue ? Is there a more recent version of the localized files ?
Maybe you could recommend me another javascript date library if nobody can find a solution.
The French culture file fr-FR:js appears to have a few of bugs. For example the regular expression for Friday shows:
/^ve(n(.(dredi)?)?)?/i
This means than either "ve" or "ven" or "ven." or "ven.dredi" are recognized as Friday but not "vendredi". More precisely the above regex matches the "vend" and leaves "redi" unmatched, thus failing the parser. The same bug is present for all days of the week and most months.
To fix this you could replace the above regular expression with:
/^ve(n(\.|(dredi)?)?)?/i
Adding the alternate "|" after the any character ".". I have also escaped the dot because it should not match "any" character but just the dot though this would not fail your test case.