Help with RegExp

S

Syed Ali

Hello,

I am trying to create a regexp to express non letters and space.
I tried using:

var myreg = new RegExp ("[^\\sA-Za-z]");

However, it is not working.
Basically I want to allow only words with letters in a textfield,
space is ok, but no special characters such as $%^ or numbers such as
1234.

If I use: var myreg = new RegExp ("[^A-Za-z]");
then "Hello" is ok, but not "Hello There" because there is a space
between Hello and There. I want to allow:

Hello
Hello There
Hello there how are you

and I do not want to allow any special characters such as #$%^ or
numbers.

Thank you!
 
L

Lasse Reichstein Nielsen

I am trying to create a regexp to express non letters and space.
I tried using:

Matches only English letters and space:

/^[a-z\s]*$/i

Matches strings with non-letter, non-space character:
/[^a-z\s]/i
var myreg = new RegExp ("[^\\sA-Za-z]");

that would be equivalent to the latter.
However, it is not working.

"not working" isn't helpful. How does it fail? It should recognize
only strings that contain a non-letter, non-space character.

So
if (myreg.test(string)) {
// string is illegal
}
should work.

/L
 
M

Michael Winter

Syed Ali wrote on 18 Dec 2003 at Thu, 18 Dec 2003 21:27:37 GMT:
Hello,

I am trying to create a regexp to express non letters and space.
I tried using:

var myreg = new RegExp ("[^\\sA-Za-z]");

However, it is not working.

I don't quite know why that doesn't work, but I'm too tired to think
about it properly anyway. :)
Basically I want to allow only words with letters in a
textfield, space is ok, but no special characters such as $%^ or
numbers such as 1234.

If I use: var myreg = new RegExp ("[^A-Za-z]");
then "Hello" is ok, but not "Hello There" because there is a
space between Hello and There. I want to allow:

Hello
Hello There
Hello there how are you

and I do not want to allow any special characters such as #$%^
or numbers.

The expression, /[^\sa-z]/i.test( string ), should evaluate to true
if any character that is not a letter (either case) or space exists
in 'string'. If it evaluates to false, all of the characters are
valid. For example,

Legal example:

if (/[^\sa-z]/i.test( 'Hello there' )) {
// Illegal characters [not executed]
} else {
// All legal characters [executed]
}


Illegal example:

if (/[^\sa-z]/i.test( 'No-one can beat me' )) {
// Illegal characters [executed: hyphen]
} else {
// All legal characters [not executed]
}

By the way, using literal regular expressions should be more
efficient. Always use a literal when you have a constant pattern.

I did test it, but blame the aforementioned tiredness if I did make
a mistake.

Mike
 
J

Janwillem Borleffs

Lasse Reichstein Nielsen said:
var myreg = new RegExp ("[^\\sA-Za-z]");

that would be equivalent to the latter.
However, it is not working.

"not working" isn't helpful. How does it fail? It should recognize
only strings that contain a non-letter, non-space character.

It's obvious why it fails; the preceding backslash causes the second
backslash to be interpreted as a literal.

This should work as expected:
var myreg = new RegExp ("[^\sA-Za-z]");


JW
 
M

Michael Winter

Janwillem Borleffs wrote on 18 Dec 2003 at Thu, 18 Dec 2003
23:04:25 GMT:
It's obvious why it fails; the preceding backslash causes the
second backslash to be interpreted as a literal.

This should work as expected:
var myreg = new RegExp ("[^\sA-Za-z]");

I thought that, however (from Netscape's JavaScript reference,
v1.3):

For example, the following are equivalent:

re = new RegExp("\\w+")
re = /\w+/

I haven't tested which of the two, double or single slash (both when
quoted), is correct.

Mike
 
L

Lasse Reichstein Nielsen

Janwillem Borleffs said:
Lasse Reichstein Nielsen said:
var myreg = new RegExp ("[^\\sA-Za-z]");
....
It's obvious why it fails; the preceding backslash causes the second
backslash to be interpreted as a literal.

As it should! The "\\" occurs inside a *string literal*. That means that
the resulting string will contain
[^\sA-Za-z]
Turned into a regular expression, it is equivalent to
/[^\sA-Za-z]/
That is just what we wanted.
This should work as expected:
var myreg = new RegExp ("[^\sA-Za-z]");

No. In a string literal, "\s" is the same as "s". That means that the
regular expression will be equivalent to /[^sA-Za-z]/, which matches
spaces. Check:
myreg.test("abc def")
It gives true, where the original poster wanted something that gave
false.

/L
 

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

Similar Threads

Help with an algorythm 5
RegEx 0
regexp and curly brackets 9
Help with #define 0
Help with datascraping script 1
Hello and Help please :-) 1
Check forms With JavaScript 1
Regexp help required please 7

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top