473,545 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with this regexp????

I have a server-side JavaScript function returning a string.
I would like to test wheather or not the string contains the following pattern:

- an equal sign,
- followed by one or more characters which are neither an ampersand nor an
equal sign,
- followed by another equal sign.

That is: A return value of that function of
X=ABCY=DEF should match, but
X=ABC&Y=DEF should not match

This is what I came up with:

if(((/=[^&=]+=/).test(get_quer y_string())) != null)
{
// matches
}
else
{
// does not match
}

The problem is that the function matches too much. For example, if
get_query_strin g() returns "LANG=EN", it matches too, although the
string contains only a single equal sign!

Any idea of what could be wrong here?

Ronald
Jul 23 '05 #1
5 1930
Ronald Fischer wrote:
I have a server-side JavaScript function returning a string.
I would like to test wheather or not the string contains the following pattern:

- an equal sign,
- followed by one or more characters which are neither an ampersand nor an
equal sign,
- followed by another equal sign.

That is: A return value of that function of
X=ABCY=DEF should match, but
X=ABC&Y=DEF should not match

This is what I came up with:

if(((/=[^&=]+=/).test(get_quer y_string())) != null)
{
// matches
}
else
{
// does not match
}

The problem is that the function matches too much. For example, if
get_query_strin g() returns "LANG=EN", it matches too, although the
string contains only a single equal sign!

Any idea of what could be wrong here?

Ronald


I don't know if there's anything wrong with the regex, I haven't gotten that far.
The reason it's matching everything is because RegExp.test() returns a boolean (two
possible values, true or false). It can _never_ return null, so the "else" code
block is _never_ executed, even when test() returns false. You also don't need so
many brackets around stuff.

Change: if(((/=[^&=]+=/).test(get_quer y_string())) != null)

to: if (/=[^&=]+=/.test(get_query _string()))

....

Now I've had a chance to look at the regex, and it seems right given the criteria
you've specified.

--
| Grant Wagner <gw*****@agrico reunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #2
Ronald Fischer wrote:
I have a server-side JavaScript function returning a string.
I would like to test wheather or not the string contains the
following pattern:

- an equal sign,
- followed by one or more characters which are neither an
ampersand nor an equal sign,
- followed by another equal sign.

That is: A return value of that function of
X=ABCY=DEF should match, but
X=ABC&Y=DEF should not match

This is what I came up with:

if(((/=[^&=]+=/).test(get_quer y_string())) != null)
For the sake of legibility, omit some parantheses, then read the
documentation of the test() method. It returns a *boolean* value
(`true' or `false') which is always not equal to `null' which is
why your test fails. You are looking for

if (/=[^&=]+=/.test(get_query _string()))

However, there are better ways to parse the query part of an URI.
[...]
The problem is that the function matches too much.


No, it does not.
PointedEars
Jul 23 '05 #3
JRS: In article <40************ ***@agricoreuni ted.com>, seen in
news:comp.lang. javascript, Grant Wagner <gw*****@agrico reunited.com>
posted at Fri, 25 Jun 2004 15:28:18 :
Ronald Fischer wrote:
I have a server-side JavaScript function returning a string.
I would like to test wheather or not the string contains the followingpattern:


Does "contains" mean "consists of only" or "has somewhere in itself" ?
If the former, change the RegExp from /=[^&=]+=/ to /^=[^&=]+=$/

But apparently not.
That is: A return value of that function of
X=ABCY=DEF should match, but
X=ABC&Y=DEF should not match

This is what I came up with:

if(((/=[^&=]+=/).test(get_quer y_string())) != null)

. ...


Better to write just

OK = /=[^&=]+=/.test("test string")

for initial test, and

OK = /=[^&=]+=/.test(get_query _string())
if (OK) { ...

for actual use; it seems clearer.
Now I've had a chance to look at the regex, and it seems right given the
criteria
you've specified.


OK by <URL:http://www.merlyn.demo n.co.uk/js-quick.htm>
OK by <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#RT>

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
Grant Wagner <gw*****@agrico reunited.com> wrote in message news:<40******* ********@agrico reunited.com>.. .
Ronald Fischer wrote:
I would like to test wheather or not the string contains the following pattern:

- an equal sign,
- followed by one or more characters which are neither an ampersand nor an
equal sign,
- followed by another equal sign.

That is: A return value of that function of
X=ABCY=DEF should match, but
X=ABC&Y=DEF should not match

This is what I came up with:

if(((/=[^&=]+=/).test(get_quer y_string())) != null)
{
// matches
}
else
{
// does not match
} The reason it's matching everything is because RegExp.test() returns a boolean (two
possible values, true or false). It can _never_ return null, so the "else" code
block is _never_ executed, even when test() returns false.


OK, got that.
Now I've had a chance to look at the regex, and it seems right given the criteria
you've specified.


Interestingly, it seems to be NEARLY right. The problem is that we need
to catch strings where some of the characters are not in the 7-Bit ASCII
character set. One example which occurs in our case is the character
with code 0xA4 (represented on our system as the so-called "internatio nal
currency symbol"). It turns out that this character does NOT match the
pattern [^&=]. Obviously, the JavaScript regexp pattern engine bails out
for those characters (maybe because of the settings of the current locale).

I wonder weather there is a portable way to catch such cases too with
a regexp.... I think that, as a temporary solution, I will have to
loop throught the string first and replace every occurence of the
offending character 0xA4 by something more harmless (fortunately, this
"loss of information" does not have any impact in my case, but it can't
be regarded as a general solution, though).

Ronald
Jul 23 '05 #5
Ronald Fischer wrote:
[...] The problem is that we need
to catch strings where some of the characters are not in the 7-Bit ASCII
character set. One example which occurs in our case is the character
with code 0xA4 (represented on our system as the so-called "internatio nal
currency symbol"). It turns out that this character does NOT match the
pattern [^&=].
It matches here. alert(/[^&=]/.test("\xA4")) yields `true' in
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8a2) Gecko/20040630
Firefox/0.8.0+.
Obviously, the JavaScript regexp pattern engine bails out
for those characters (maybe because of the settings of the
current locale).
Possibly.

J(ava)Script strings are Unicode strings (more exact: UTF-16 strings,
as [W3C] DOMStrings are), but only from JavaScript version 1.3 on and
AIUI from JScript version 5.5 on. The Unicode character \u00A4 is the
same as \xA4 in ISO-8859-1 (Latin-1) because Unicode shares code points
\xA0 (\u00A0) to \xFF (\u00FF) with that encoding. However, the two
characters should differ if your locale is not UTF-xx and not
ISO-8859-1. For example, \xA4 should equal \u20AC (the Euro sign) in
ISO-8859-15 (Latin-9).

Interestingly, I have LC_ALL=de_DE@eu ro here, yet \xA4 and \u20AC differ
in my UA which is said to interpret JavaScript 1.5. In that language,
AFAIS in contrast to ECMAScript 3, it is specified that \xA4 means code
point 0xA4 in ISO-8859-1 which is not equal to \u20AC (so my Mozilla is
correct here, however the implementation is IMHO not standards
compliant in this regard as it is not locale-aware). According to the
JScript Reference, \xhh refers to "ASCII characters" there which would
mean only \x00 to \x7F to be valid escape sequences. That would remove
the locale dependency but I am afraid that they meant "Extended ASCII
characters" rather than "US-ASCII characters", which would re-introduce it.
I wonder weather there is a portable way to catch such cases too
with a regexp....


You can use alternation to include characters you require to be matched:

/=([^&=]|\xA4)+=/.test(...)

Use character classes if there is more than one character, e.g.:

/=([^&=]|[\xA0-\xFF])+=/.test(...)
PointedEars
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1762
by: Lukas Holcik | last post by:
Hi Python crazies!:)) There is a problem to be solved. I have a text and I have to parse it using a lot of regular expressions. In (lin)u(ni)x I could write in bash: cat file | sed 's/../../' | sed 's/../../' .. .. .. > parsed_file I write a parser in python and what I must do is: regexp = re.compile(..)
6
1567
by: Wesley Groleau | last post by:
Actually, 95% of this is copied from something else that supposedly works. (And from what I know of perl, I thought it would work, too.) But in a loop, print $_ . "\n"; # first line is "0 HEAD" # second is "1 SOUR hand-edited blah blah ...." # Parse record
2
4048
by: timmy_dale12 | last post by:
Im implementing a calendar and cant figure out what this method does or how it works // datetime parsing and formatting routimes. modify them if you wish other datetime format function str2dt (str_datetime) { var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/; if (!re_date.exec(str_datetime)) return alert("Invalid Datetime...
1
1733
by: Pavils Jurjans | last post by:
Hallo, I yesterday was browsing the book, JavaScript The Definitive Guide (4th ed), which is, unquestionably, the best reference book for JS. To my surprise, I didn't find any documentation about the static properties of global RegExp object, ie, RegExp.lastMatch, RegExp.leftContext, RegExp.rightContext, and all those RegExp.$x properties....
1
1483
by: jgabbai | last post by:
Hi, I have data in a row, such as "34,54,12,134" I need to select rows where they match certain numbers, and not others, so match 34,12 and not 134 (ie the above row would fail, but "34,124,12" would pass). How do I build a REGEXP to search this - I've tried (^34|,34,|,34$) - but that only works to find one match. How do I "AND"
8
2007
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty string (when you don't enter any values) - this is wrong d+ expression does not match, for example "g24" string - this is also wrong ...
7
3427
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For example: var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/ var text = "There were some nesting parens in the test"; alert (regExpPos (text,...
11
3542
by: Flyzone | last post by:
Hello, i have again problem with regexp :-P I need to match all lines that contain one word but not contain another. Like to do "grep one | grep -v two:" The syntax of the string is: (any printable char)two:(any printable char)one(any printable char) Example: Apr 30 00:00:09 v890neg0 two: findings: blablabla
34
4443
by: Jorge | last post by:
Why is it listed here : http://www.crockford.com/javascript/recommend.html under "deprecation" ? Without it, how is an anonymous function() going to call itself (recursively) ? I use to write timers like this :
0
7656
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. ...
0
7808
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...
1
7423
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...
0
7757
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...
0
5972
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...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
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...
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
704
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...

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.