By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

help on regular expression

P: n/a
Hi,
I'd like to have a regular expression that validates everything but
blank values (so at least one character) and non alphanumeric
characters. How would I do it ?

thanks

Aug 25 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a

sa*************@googlemail.com wrote:
I'd like to have a regular expression that validates everything but
blank values (so at least one character) and non alphanumeric
characters. How would I do it ?
This site is a good reference for RegEx:
http://regular-expressions.info/index.html

Someone else can probably correct my limited experience of expressions,
but this one here seems to satisfy your criteria:

^\w*$

Aug 25 '06 #2

P: n/a
Hi Bobbo,
The thing is that I've been trying different combination, including
yours, but i always end up with the following error :

Expected '/'

what is that all about ??

Aug 25 '06 #3

P: n/a

samuelberthe...@googlemail.com wrote:
The thing is that I've been trying different combination, including
yours, but i always end up with the following error :

Expected '/'
Can you post some code please? And tell us the line on which the error
occurs.

Aug 25 '06 #4

P: n/a
here it is:

var match = eval(me.RegExpr).exec(strParams);
me.RegExpr has the value that you gave me earlier on. strParams has
value 'test'.
any idea with i get this error ?

Bobbo wrote:
samuelberthe...@googlemail.com wrote:
The thing is that I've been trying different combination, including
yours, but i always end up with the following error :

Expected '/'

Can you post some code please? And tell us the line on which the error
occurs.
Aug 25 '06 #5

P: n/a

samuelberthe...@googlemail.com wrote:
var match = eval(me.RegExpr).exec(strParams);

me.RegExpr has the value that you gave me earlier on. strParams has
value 'test'.
You probably need to explicitly construct an object first:
var re = new RegExp(/^\w*$\i);

Or something.

Aug 25 '06 #6

P: n/a

Bobbo wrote:
var re = new RegExp(/^\w*$\i);
Oops, my bad:
var re = new RegExp(/^\w*$/i);

Aug 25 '06 #7

P: n/a
sa*************@googlemail.com wrote in comp.lang.javascript:

var match = eval(me.RegExpr).exec(strParams);
me.RegExpr has the value that you gave me earlier on. strParams has
value 'test'.
any idea with i get this error ?

Use the methods involving RE literals (patterns) associated with strings
when using REs to look for patterns in strings:
index = yourString.search(/anRE/);
arrayOfMatches = yourString.match(/anRE/);

Read the section on core Java/ECMAscript about strings and their methods.

http://www.webreference.com/javascri...ef/string.html
Aug 25 '06 #8

P: n/a
JRS: In article <11**********************@75g2000cwc.googlegroups. com>,
dated Fri, 25 Aug 2006 08:30:30 remote, seen in
news:comp.lang.javascript, Bobbo <ro******@gmail.composted :
>
Bobbo wrote:
>var re = new RegExp(/^\w*$\i);

Oops, my bad:
var re = new RegExp(/^\w*$/i);
Have you ever considered testing code before you post it? That may not
find all errors, but it should at least check that the code works
sometimes.
To accept strings of at least one character, in which every character is
in the set 0-9 a-z A-Z _ :-
OK = /^\w+$/.test(Str)

To not accept _ :-
OK = /^[0-9a-z]+$/i.test(Str)

OP : read the newsgroup and its FAQ.
--
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 25 '06 #9

P: n/a

Dr John Stockton wrote:
Have you ever considered testing code before you post it? That may not
find all errors, but it should at least check that the code works
sometimes.
As it happens, I always check that code works before I post it. In
this case, I had something I needed to keep on the clipboard and
mistyped it.

Aug 29 '06 #10

P: n/a
Thank you John,
Your solution worked. I haven't checked the FAQ, I'm using Google
Groups so I'm not sure where to check for this FAQ..

Thank you again.

Aug 29 '06 #11

P: n/a
John,
I've tried your solution. It almost works. In fact I need to authorise
spaces in the string, but the string cannot be made up of spaces only
(at least one non-blank character). How can I modify this ?

Thank you

Aug 29 '06 #12

P: n/a
sa*************@googlemail.com said the following on 8/29/2006 5:43 AM:
Thank you John,
Your solution worked. I haven't checked the FAQ, I'm using Google
Groups
Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

<URL: http://www.safalra.com/special/googlegroupsreply/ >
so I'm not sure where to check for this FAQ..
comp.lang.javascript FAQ:

<URL: http://jibbering.com/faq>

A search for FAQ in this group will turn up that URL.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 29 '06 #13

P: n/a
Thanks a lot for those tips Randy, I ' ll do so from now on.
Randy Webb wrote:
sa*************@googlemail.com said the following on 8/29/2006 5:43 AM:
Thank you John,
Your solution worked. I haven't checked the FAQ, I'm using Google
Groups

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

<URL: http://www.safalra.com/special/googlegroupsreply/ >
so I'm not sure where to check for this FAQ..

comp.lang.javascript FAQ:

<URL: http://jibbering.com/faq>

A search for FAQ in this group will turn up that URL.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 29 '06 #14

P: n/a
JRS: In article <11*********************@74g2000cwt.googlegroups.c om>,
dated Tue, 29 Aug 2006 02:43:50 remote, seen in
news:comp.lang.javascript, sa*************@googlemail.com posted :
>Thank you John,
Your solution worked. I haven't checked the FAQ, I'm using Google
Groups so I'm not sure where to check for this FAQ..
I did give you the link in my signature.

Also, every day, an article titled "FAQ Topic - ..." is posted in the
newsgroup, and its signature links to the FAQ.

If you do a Google search of the Web for +comp.lang.javascript +FAQ you
should also find it fairly easily, but I've not tried that lately.
Do you need more?

OP : read the newsgroup and its FAQ.
--
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 29 '06 #15

P: n/a
JRS: In article <11*********************@i42g2000cwa.googlegroups. com>,
dated Tue, 29 Aug 2006 02:47:49 remote, seen in
news:comp.lang.javascript, sa*************@googlemail.com posted :
>I've tried your solution. It almost works. In fact I need to authorise
spaces in the string, but the string cannot be made up of spaces only
(at least one non-blank character). How can I modify this ?
OK = /^[\w ]+$/.test(Str) && /\w/.test(Str)

The first part allows spaces, the second part requires at least one non-
space.

OK = /^\w[\w ]*$/.test(Str)

The first character must be a word character, the rest may also be
spaces.

OK = /^\w[\w ]*\w$/.test(Str)

The first and last must be word characters, the rest must be either.
The first must not be last.

Test those, both for accepting & for rejecting all & only what you want.
--
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 29 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.