daveVk

msg:4407552 | 3:55 am on Jan 17, 2012 (gmt 0) |
Start by removing all spaces value=value.replace( /\s/g, "" ); And finally reconstruct it, if need be var parts = value.split('-'); value = parts.join(' - '); Consider doing simpler regex on parts[0] and parts[1].
|
nelsonm

msg:4407553 | 4:04 am on Jan 17, 2012 (gmt 0) |
Hi, I actually didn't think of removing all then reconstructing. I thought there was a way to do it in the expression and maybe there is but still... Nice! What do you mean... | Consider doing simpler regex on parts[0] and parts[1]. |
| How can it be simpler?
|
daveVk

msg:4407574 | 7:14 am on Jan 17, 2012 (gmt 0) |
Probably can be done in expression, but I prefer keeping it simple. You could test both parts with var regexp = /^(1[012]|[1-9])[a|p]m$/i; and report which part[s] are in error.
|
nelsonm

msg:4407765 | 4:20 pm on Jan 17, 2012 (gmt 0) |
I realized i made one mistake on my expression. If i want the expression to recognize a space, i have to use "\s" not " ". So the corrected expression should be: var regexp = /^(1[012]|[1-9])[a|p]m\s-\s(1[012]|[1-9])[a|p]m$/i; I'm not too well versed on regular expressions so with respect to... You could test both parts with var regexp = /^(1[012]|[1-9])[a|p]m$/i; and report which part[s] are in error. |
| I think you mean altering my original expression to "/(1[012]|[1-9])[a|p]m/i" which will look for all occurrences of (1 thru 12) with am or pm in the input string. So, If i understand you correctly... i would construct the expression as follows var regexp = /(1[012]|[1-9])[a|p]m/i; then test for and get the parts... parts = value.exec(regexp); If the array is of at least a length of 2, i have at least two valid parts - then join the first two part - ignoring the anything else... value = parts[0] + ' - ' + part[1]; At this point, i have what i need. Do i have it right?
|
nelsonm

msg:4407849 | 6:38 pm on Jan 17, 2012 (gmt 0) |
i meant... parts = value.match(regexp);
|
daveVk

msg:4407961 | 12:27 am on Jan 18, 2012 (gmt 0) |
I had in mind something like below var parts = value.split('-'); if ( parts.length !== 2 ) { ... return error } if ( !( regexp.test( part[0] ) ) ) { ... return error } if ( !( regexp.test( part[1] ) ) ) { ... return error } value = parts.join(' - '); ... return Ok In this case I think var regexp = /^(1[012]|[1-9])[a|p]m$/i; is Ok, no expert on these.
|
g1smd

msg:4407965 | 12:48 am on Jan 18, 2012 (gmt 0) |
Yes, this is a task that should be done in several steps: remove spaces and punctuations, check the remainder is a valid string, format the data for display.
|
nelsonm

msg:4407970 | 1:23 am on Jan 18, 2012 (gmt 0) |
Thanks a lot, you've been a great help!
|
|