Welcome to WebmasterWorld Guest from 50.16.24.12

Forum Moderators: open

trying to control spaces with regex.

   
8:14 pm on Jan 16, 2012 (gmt 0)

5+ Year Member



hi all,

I have the following function that uses a regular expression to match a "00am - 00am" time format. Some examples would be; "8am - 12pm" or "10am - 2pm".

function checkTimeBlock(value, colname){
var value, colname, regexp, data;
var regexp = /^(1[012]|[1-9])[a|p]m - (1[012]|[1-9])[a|p]m$/i;

if ( !( regexp.test( value ) ) ) {
return [false,colname + ': Invalid format, try "00am - 00pm".'];
}else{
return [true,""];
}
}


It works but it's not very flexible. I'd like to also do the following:
1. remove any leading and trailing spaces.
2. remove any spaces from between "00" and "am" or "00" and "pm".
3. make sure that only one space is on either side of the dash so that " -" or "- " or "-" gets converted into " - ".
4. replace Upper case "AM" or "PM" to lower case "am" or "pm".

Can this be done in the expression?

thanks.
3:55 am on Jan 17, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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].
4:04 am on Jan 17, 2012 (gmt 0)

5+ Year Member



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?
7:14 am on Jan 17, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.
4:20 pm on Jan 17, 2012 (gmt 0)

5+ Year Member



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?
6:38 pm on Jan 17, 2012 (gmt 0)

5+ Year Member



i meant...

parts = value.match(regexp);
12:27 am on Jan 18, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.
12:48 am on Jan 18, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.
1:23 am on Jan 18, 2012 (gmt 0)

5+ Year Member



Thanks a lot, you've been a great help!
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved