473,387 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Regular Expressions

Please help me understand regular expressions. I have read different
articles about them, they all seem to explain it differently.

1.
On one site it says to do it like this:
return ( /^[0-9]+$/.test(num) )

2.
On another it says:
var regExp = new RegExp(/^[a-zA-Z0-9]+$/)
return ( regExp.test(str) )

3.
I see others that put the expression into a string and then test it.

I have tried both 1 and 2, and on some clients 2 will work, but on
others it always returns false no matter what. What's the difference
between them? How do I know what is going to work on all clients?

Is there a way to find out exactly what version of Javascript is being
used on the client. Not from code, but just by looking somewhere on
the machine. A lot of what I am trying to write this for is websites
to be used internally.

I appreciate your time.
Kalvin

Jul 23 '05 #1
7 1521
Damn! I just started to rely on Regular Expressions ... what clients are
causing the problem? I don't actually use parentheses like you do, and I
think you are supposed to use quotes when doing the new RegExp thing.

1.
return /^[0-9]+$/.test(num)
2.
var regExp = new RegExp ("^[a-zA-Z0-9]+$")
return regExp.test(str)

Kalvin wrote:
1.
return ( /^[0-9]+$/.test(num) )
2.
var regExp = new RegExp(/^[a-zA-Z0-9]+$/)
return ( regExp.test(str) ) I have tried both 1 and 2, and on some clients 2 will work, but on
others it always returns false no matter what. What's the difference
between them? How do I know what is going to work on all clients?

Jul 23 '05 #2
Apart from really old browsers the only difference I am aware of is
that IE 5.0 doesn't support non-capturing parenthesis eg: (?:banana)
There may be more differences but I haven't come across any myself.

Jul 23 '05 #3
Apart from really old browsers the only difference I am aware of is
that IE 5.0 doesn't support non-capturing parenthesis eg: (?:banana)
There may be more differences but I haven't come across any myself.

Jul 23 '05 #4
We use IE 6 and Netscape. In our company we use an image to rebuild
machines, and so far have only come across one that example 2 just will
not work. I used example 1 and it worked fine, so far. I really like
regular expressions, and hope I don't have other problems later.

Thank you everyone for your replies.

BTW, is there a way to discover what version of javascript is on a
machine?

Kalvin

Jul 23 '05 #5
Both ways of using the regulare expression are the same ... the first
method is more consise and the RegExp object is created on-the-fly.
The 2nd method is using a pre-defined RegExp object.

If you're getting different results from different clients, it's
probably because of the differences in the JavaScript engines. You can
find out which version buy the client's version (like Firefox is
implementing JavaScript 2 ... just an example .. i'm not sure which
version it's implementing).

a good resource on javascript:

http://www.devguru.com/Technologies/...ipt_index.html

Jul 23 '05 #6
Kalvin wrote:
[snip]

var regExp = new RegExp(/^[a-zA-Z0-9]+$/)
return ( regExp.test(str) )


return new RegExp("^[a-z0-9]+$","i").test(str);

Is the preferred construct, I believe.
Mick

[snip]
Jul 23 '05 #7
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Tue, 1 Feb 2005 07:25:35, seen in news:comp.lang.javascript,
Kalvin <kt***@streck.com> posted :

BTW, is there a way to discover what version of javascript is on a
machine?


<URL:http://www.merlyn.demon.co.uk/js-other.htm>
<URL:http://www.merlyn.demon.co.uk/js-tests.htm>

But such tests do not necessarily work on all versions.

Read newsgroup FAQ 4.26 and its Notes.

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

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

Similar topics

8
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.