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

Help with RegExp

P: n/a
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!
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
al**@paul.rutgers.edu (Syed Ali) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #3

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:wu**********@hotpop.com...
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

Jul 20 '05 #4

P: n/a
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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #5

P: n/a
"Janwillem Borleffs" <jw@jwscripts.com> writes:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:wu**********@hotpop.com...
> 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.