help on regular expression

S

samuelberthelot

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
 
B

Bobbo

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*$
 
S

samuelberthelot

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 ??
 
S

samuelberthelot

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 ?
 
B

Bobbo

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 25 Aug 2006 08:30:30 remote, seen in
news:comp.lang.javascript said:
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.
 
P

Patient Guy

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/javascript/reference/core_ref/string.html
 
B

Bobbo

Dr said:
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.
 
S

samuelberthelot

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.
 
S

samuelberthelot

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
 
R

Randy Webb

(e-mail address removed) 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.

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 29 Aug 2006 02:43:50 remote, seen in
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 29 Aug 2006 02:47:49 remote, seen in
news:comp.lang.javascript, (e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top