473,770 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pattern matching

I ask the user to enter a time in the formatio 12:30 PM. onChange I
send the string to this function. I'm using alert boxes to test
it...and am always getting the "Does not work" alert box. What am I
doing wrong?

function checkTime(patte rn){
if(pattern.valu e.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {
alert ("works")
}
else alert(pattern.v alue + "Does not work")
}
Jul 23 '05 #1
5 2188
Abby Lee wrote on 02 sep 2004 in comp.lang.javas cript:
I ask the user to enter a time in the formatio 12:30 PM. onChange I
send the string to this function. I'm using alert boxes to test
it...and am always getting the "Does not work" alert box. What am I
doing wrong?

function checkTime(patte rn){
if(pattern.valu e.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {
alert ("works")
}
else alert(pattern.v alue + "Does not work")
}


Use test, it gives true or false.

You have to read up on regex syntax,
the + is not what you think.

"Does not work" works like a red screen on a bull in this NG.

Why pattern.value?
function checkTime(patte rn){
if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )
alert ("correct")
else
alert(pattern + " is incorrect")
}

btw, why not have your users use a 24 hour clock?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
Evertjan. wrote:

function checkTime(patte rn){
if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )
alert ("correct")
else
alert(pattern + " is incorrect")
}


This would allow: 99:99Am .....

/^((0?[1-9])|(1[012])):[0-5]\d\s?[ap]m$/i
Not tested.
Mick
Jul 23 '05 #3
Mick White wrote on 02 sep 2004 in comp.lang.javas cript:
Evertjan. wrote:

function checkTime(patte rn){
if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )
alert ("correct")
else
alert(pattern + " is incorrect")
}


This would allow: 99:99Am .....

/^((0?[1-9])|(1[012])):[0-5]\d\s?[ap]m$/i
Not tested.


Sure, but there is a difference between correcting the OP's syntax
and improving on his idea/specs.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #4
JRS: In article <Xn************ ********@194.10 9.133.29>, dated Thu, 2
Sep 2004 17:12:43, seen in news:comp.lang. javascript, Evertjan.
<ex************ **@interxnl.net > posted :
Abby Lee wrote on 02 sep 2004 in comp.lang.javas cript:
I ask the user to enter a time in the formatio 12:30 PM.
function checkTime(patte rn){
if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )

if (/^\d{1,2}:\d{2} [ap]m$/i.test(pattern) ) // to be exact
if (/^[01]?\d:\d{2} [ap]m$/i.test(pattern) ) // hours 0-9,00-19
if (/^([1-9]|1[012]):\d{2} [ap]m$/i.test(pattern) ) // hours 1-12

The minutes field can be checked with, instead of \d{2}, [0-5]\d - then
we have an expression that allows only well-formed alien times (ISTM).

In the last form above, one can insert either 0 or 0? before the first [
..

Part-tested.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5


Abby Lee wrote:
I ask the user to enter a time in the formatio 12:30 PM. onChange I
send the string to this function. I'm using alert boxes to test
it...and am always getting the "Does not work" alert box. What am I
doing wrong?

function checkTime(patte rn){
if(pattern.valu e.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {
This regular expression matches:

- a single digit /[0-9]/
- one or more spaces / +/
- a space / /
- a colon /:/
- a single digit /[0-9]/
- one or more spaces / +/
- a space / /
- the letters a, p, A, or P /[aApP]/
- one or more spaces / +/
- a space / /
- the letters m or M /[mM]/

Is this really what you want?

I'm guessing you think the "+"s are concatenation or something, used to
"assemble" the regular expression. They aren't required, and in fact,
the "+" has a special meaning in regular expression syntax, so it
doesn't even match a "+" character.

You probably want something like:

if (/^\s*(1[012]|[1-9]):[0-5][0-9] ?[ap]m/i.test(pattern. value)) {

This matches (case-insensitively):

- from the beginning of the line /^/
- zero or more whitespace /\s*/ - accounts for any spaces the user
might enter
- the digit "1" /1/ followed by the digits 0, 1 or 2 [012]; OR
- the digits 1 to 9 /[1-9]/
- a colon /:/
- the digits 0 to 5 /[0-5]/
- the digits 0 to 9 /[0-9]/
- an optional space / ?/ - in case your users don't enter one
- the letters a or p /[ap]/
- the letter m /m/

It should match:

1:00am
12:30 PM
4:37 Am

I haven't tested it thoroughly, but it seems to do what you want.
alert ("works")
}
else alert(pattern.v alue + "Does not work")
}


--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6981
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just use pattern matching to determine if the proper amount of numeric characters has been met. Here is the function I've already done. Any help you can give in a pattern matching solution would be much appreciated and very educational.
176
8185
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? Thank you, -- greetz tom
9
3217
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images files from gif # format to png format. Now you need to change the # html code to use the .png files. So, essentially
1
2738
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001 94100 If I run a pattern matching search for the string "940", then I should get the top two rows of data back. If I run a pattern matching search for the string "94", then I should get all the three rows of data back.
10
4984
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement, knowledge of fast and practical algorithms is not commonplace." Hume and Sunday, "Fast String Searching", Software - Practice and Experience, Vol. 21 # 11, pp 1221-48
5
5758
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I have a list, as below: sentence = "the color is $red" patterns = pos = sentence.find($)
9
5067
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x matches abc
2
3398
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and I'd like the attention of fp as well as C++ gurus if available. The language I'm implementing - PILS - is dynamically
1
2770
by: VanKha | last post by:
I write this program for pattern-matching,but it gives wrong result: #include<iostream> #include<conio.h> #include<string.h> using namespace std; main() { char text,pat; cout<<"text:";cin>>text;
5
5050
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a file: //This is my source file //this is where i want to insert a line class Class1
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.