473,795 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2059
al**@paul.rutge rs.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(str ing)) {
// string is illegal
}
should work.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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.******@blueyo nder.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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #5
"Janwillem Borleffs" <jw@jwscripts.c om> 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/rasterTriangleD OM.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
2356
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. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
4
2215
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 = " s^\?AAA\?01^BBB^g; #Comment "
8
5109
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 = document.createElement('a'); link.href ="javascript:show_calendar('document.form1.date',document.form1.date.value);"; img = document.createElement('img'); img.setAttribute("src","H:Diverse\\cal.gif"); link.appendChild(img);
5
1287
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 function that checks for each of these and still validates the rest as a number: function checkNumber( an_item ) { count_val = an_item.value;
4
7478
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 "PRACTICO". What's the best way to do this? TIA,
3
3007
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 is somewhat limited to say the least, so I'm struggling with making it work as I wand. What I have is an associative array like this: smileys = 'smile.gif'; smileys = 'sad.gif';
7
1730
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" or any < sign? }
3
1882
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 the hard coded regular expression. but if I set $text to 12499, it will return 1 for a match. $text = str_repeat('*', 12499);
5
4960
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.