473,320 Members | 1,990 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,320 software developers and data experts.

Help with RegExp

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
5 2021
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
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

"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
8
by: timmy_dale12 | last post by:
I need help with this one. I have a function that pastes a row. In each row i am pasting a link which is to call a date picker javascript function. This is the code that pastes the link : link =...
5
by: Iain Downie | last post by:
Dear Group, I have a text field that I wish to cellect numbers of bird seen. However, some folk want to use 'c' before the number or '+' after to indicate a fuzzy number. I have tried creating a...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
3
by: Roy W. Andersen | last post by:
Hi, I need to do some replace-calls on certain strings in order to replace smiley glyphs and other keywords with graphical icons on the client. Unfortunately, my knowledge of regular expressions...
7
by: VUNETdotUS | last post by:
How can I get the text after matching string is found: var str = "1111>AAAA<2222>BBBB<3333>CCCC"; if(str.indexOf("2222>")){ //how to get "BBBB" value following my "2222>" but before "<3333"...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.