Welcome to WebmasterWorld Guest from 23.20.6.20

Forum Moderators: open

Message Too Old, No Replies

trying to control spaces with regex.

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

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 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].
4:04 am on Jan 17, 2012 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 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?
7:14 am on Jan 17, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 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.
4:20 pm on Jan 17, 2012 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 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?
6:38 pm on Jan 17, 2012 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


i meant...

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 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.
12:48 am on Jan 18, 2012 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 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.
1:23 am on Jan 18, 2012 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members