homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
trying to control spaces with regex.
nelsonm

5+ Year Member



 
Msg#: 4407415 posted 8:14 pm on Jan 16, 2012 (gmt 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.

 

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4407415 posted 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

5+ Year Member



 
Msg#: 4407415 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4407415 posted 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

5+ Year Member



 
Msg#: 4407415 posted 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

5+ Year Member



 
Msg#: 4407415 posted 6:38 pm on Jan 17, 2012 (gmt 0)

i meant...

parts = value.match(regexp);

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4407415 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4407415 posted 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

5+ Year Member



 
Msg#: 4407415 posted 1:23 am on Jan 18, 2012 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

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