regex help: alphanumeric with optional dashes

E

enrique

I'm trying to debug my expression that matches an alphanumeric with any
number of dashes (including none), except at the ends and obviously
disallowing two or more consecutive dashes.

Here it is: /\w+(-?\w+)*/

Test cases that I expect to pass are failing:

"01234abcdef" true

"0123-412-8370" false (should've been "true")

"asdkfjakfj" true

"0-1" false (should've been "true")

"-" false

"--" false

"-ABC123" false

"00230-" false

"ABC-123" false (should've been "true")

"1-" false

"111223333" true

Would anyone lend a hand? Thank you.
 
L

Lasse Reichstein Nielsen

enrique said:
I'm trying to debug my expression that matches an alphanumeric with any
number of dashes (including none), except at the ends and obviously
disallowing two or more consecutive dashes.

Didn't I see you in the Java group. Even deleted my answer because I
answer as if it was a Javascript question. I feel ... vindicated :)
Here it is: /\w+(-?\w+)*/

It should match the examples you give. Actually, it matches and
string with a word character in it, so it should probably be anchored.
Also, the "?" isn't necessary. So, try this:

/^\w+(-\w+)*$/
Test cases that I expect to pass are failing: ....
"0123-412-8370" false (should've been "true")

It is true for me. Show us some more code, and tell us which browser
you use.
"asdkfjakfj" true

"0-1" false (should've been "true")

Is true for me. What is the rest of your code, and what browser are
you using?
"-ABC123" false

Should be true for the provided regular expression (and is for me).

Try writing this in the address line:
javascript:alert(/\w+(-?\w+)*/.test("-ABC123"))
"00230-" false

"ABC-123" false (should've been "true")

"1-" false

Should all be true for the provided regexp.
Would anyone lend a hand?

You are doing something wrong. It's impossible to say what, since you
haven't told us what you do :) The regular expression, while not perfect,
should not give the results you report, and doesn't in my browsers.

/L
 
E

enrique

Didn't I see you in the Java group. Even deleted my answer because I
answer as if it was a Javascript question. I feel ... vindicated :)

Yup, I'm busted. I'm such a dope; wrong usenet group. I think I can
safely un-post from the other group now that you have taken you reply
out.

I've been testing with Firefox. I've been storing test cases in an
array, and then iterating over this collection and calling a validation
method that makes use of the expression above. Here's the test
harness:

<script type="text/javascript">
function IsAccountNumber(strString)
{
var strValidChars = /\w+(-?\w+)*/;
return validateString(strString, strValidChars);
}
var tests = new Array("01234abcdef", "0123-412-8370", "asdkfjakfj",
"0-1", "-", "--", "-ABC123", "00230-", "ABC-123", "1-", "111223333");
for (i = 0, j = tests.length; i < j; ++i)
{
document.writeln("<p>&quot;<strong>" + eval("tests") +
"</strong>&quot; <em>" + eval("IsAccountNumber(tests)") +
"</em></p>");
}
</script>
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Thu, 19 May 2005 09:12:59, seen in
enrique said:
I'm trying to debug my expression that matches an alphanumeric with any
number of dashes (including none), except at the ends and obviously
disallowing two or more consecutive dashes.

Here it is: /\w+(-?\w+)*/


/^\w+(-\w+)*$/

?


For proper quoting when using Google for News :-
Keith Thompson wrote in comp.lang.c, message ID
<[email protected]> :-
If you want to post a followup via groups.google.com, don't use
the "Reply" link at the bottom of the article. Click on "show options"
at the top of the article, then click on the "Reply" at the bottom of
the article headers.
 
L

Lasse Reichstein Nielsen

enrique said:
I've been testing with Firefox. ....
Here's the test harness:

<script type="text/javascript">
function IsAccountNumber(strString)
{
var strValidChars = /\w+(-?\w+)*/;
return validateString(strString, strValidChars);

So this is where it happens ... except that we can't see the
validateString method :)
}
var tests = new Array("01234abcdef", "0123-412-8370", "asdkfjakfj",
"0-1", "-", "--", "-ABC123", "00230-", "ABC-123", "1-", "111223333");
for (i = 0, j = tests.length; i < j; ++i)
{
document.writeln("<p>&quot;<strong>" + eval("tests") +
"</strong>&quot; <em>" + eval("IsAccountNumber(tests)") +
"</em></p>");


Overuse of eval, but otherwise I can't see a problem with this. This
works just the same, and doesn't reparse the string on every pass
through the loop:

document.writeln("<p>&quot;<strong>" + tests +
"</strong>&quot;<em>" + IsAccountNumber(tests) +
"</em></p>");

Never use "eval" :)

/L
 
E

enrique

Gosh, I'm a dope! The trouble is not even occurring in my regex; it's
happening in my definition of "validateString".

Thanks very much for confirming the expression with your own testing.
 
E

enrique

Fill me on the "eval" function and your discouraging its use, please.

Thanks again.

epp
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 20
May 2005 14:35:21, seen in Evertjan.
enrique wrote on 20 mei 2005 in comp.lang.javascript:
Fill me on the "eval" function and your discouraging its use, please.

[Please quote, this is not email, but usenet. Thousands of users are
reading too.]

The archive of this NG has it all:

<http://groups-beta.google.com/groups?num=100
&q=eval.is.evil&as_ugroup=comp.lang.javascript>

mind the wordwrap trap

Good News software can post - and display - it without wrapping :

<http://groups-beta.google.com/groups?num=100>&q=eval.is.evil&as_ugroup=comp.lang.javascript>

The following should be understood as a unit be any reasonable
newsreader :-

<URL:http://groups-beta.google.com/groups?num=100>
&q=eval.is.evil&as_ugroup=comp.lang.javascript>


Please don't assume that everyone is reading News on-line; for off-line
readers a reference to the FAQ (which an off-line newsreader should
hold) is more helpful. You could even include a reference in your Sig.

No doubt Google has it all; but a lot of what it has is not worth
reading.

Function eval can be useful with form input :

var k = eval(<form-element>.value)

allows the Americans to enter distances correctly by typing the
distance in feet followed by *0.3048 .

Well, nearly correctly : eval("3*0.3048") -> 0.9144000000000001,
explanation obvious.
 
M

Michael Winter

D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 20 May 2005 23:12:32, seen in
Michael Winter said:
JRS: In article <[email protected]>, dated Fri, 20
May 2005 14:35:21, seen in Evertjan.
<[email protected]> posted :
[snip]

The following should be understood as a unit be any reasonable
newsreader :-

<URL:http://groups-beta.google.com/groups?num=100>
&q=eval.is.evil&as_ugroup=comp.lang.javascript>

That would be true, if you pressed Delete once more - the extra bracket
has spoilt your demonstration. :) The same is true of the other URL.


Agreed, thanks. It was not possible for me to those lines that in the
article-being-edited.



I've introduced the improved code display and also CSS throughout my
site now; would some of you like to look at a few pages to see whether
they work for systems unlike mine?



Question : How should CSS and javascript document.bgColor='red'
interact? Introducing CSS

<link rel="StyleSheet" href="styles-a.css" type="text/css">

containing body { color:black; background:#FFFFCC }

stops it working in my IE4, since the timed change of background colour
is fundamental and does not occur.
 
M

Michael Winter

On 21/05/2005 21:32, Dr John Stockton wrote:

[snip]
I've introduced the improved code display and also CSS throughout my
site now; would some of you like to look at a few pages to see whether
they work for systems unlike mine?

There don't seem to be any immediate issues. If there are, I imagine
them to be limited to older user agents, and differences would be
relatively insignificant.

That said, I opened <URL:http://www.merlyn.demon.co.uk/js-randm.htm> in
NN 4.77 and, after receiving messages about missing includes, the
instance promptly closed without any messages. I didn't see any close
method calls in the vicinity.

Staying with that particular document, you have a name-only reference to
'TestBox' in the speed tests section. Gecko-based user agents don't like
that very much.
Question : How should CSS and javascript document.bgColor='red'
interact? [...]

I'd imagine, not very well. The usual way to override a style sheet
setting is through the style object of an element as that is considered
the highest form of specificity. You would be better off with

document.body.style.backgroundColor = 'red';

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 22 May 2005 14:07:57, seen in
Michael Winter said:
On 21/05/2005 21:32, Dr John Stockton wrote:

[snip]
I've introduced the improved code display and also CSS throughout my
site now; would some of you like to look at a few pages to see whether
they work for systems unlike mine?

There don't seem to be any immediate issues. If there are, I imagine
them to be limited to older user agents, and differences would be
relatively insignificant.

That said, I opened <URL:http://www.merlyn.demon.co.uk/js-randm.htm> in
NN 4.77 and, after receiving messages about missing includes, the
instance promptly closed without any messages. I didn't see any close
method calls in the vicinity.

I don't close it intentionally. When I try without the include file,
there's no javascript running, it complains every time I call a function
from the include files during page load, and none of the buttons work -
which is not surprising.

I don't support Netscape 4 on the site, because I never had DynWrite
working in it, and I have no access to N4 anyway now. But that page
does not use DynWrite.

Staying with that particular document, you have a name-only reference to
'TestBox' in the speed tests section. Gecko-based user agents don't like
that very much.

I take it that you mean that TestBox.value should be
document.forms["xxx"].TestBox.value ? I've dealt with that, for
TestBox and its friends. It's an error that MSIE does not see, so there
are probably still a few more like that lurking on other pages, alas.

Question : How should CSS and javascript document.bgColor='red'
interact? [...]

I'd imagine, not very well. The usual way to override a style sheet
setting is through the style object of an element as that is considered
the highest form of specificity. You would be better off with

document.body.style.backgroundColor = 'red';

Indeed I am, thanks; js-alarm.htm is the last of the more-or-less normal
pages to be CSS-upgraded, I think.
 
M

Michael Winter

I don't support Netscape 4 on the site [...]

And I don't necessarily suggest you do. I just took a look as NN4 can
react badly to some style sheet content. However, I'm pretty certain
that the closure isn't caused by the style sheet as it's too simple.

[snip]
I take it that you mean that TestBox.value should be
document.forms["xxx"].TestBox.value ?

That, or use an id with getElementById (and appropriate emulation for IE4).
[...] It's an error that MSIE does not see, so there are probably
still a few more like that lurking on other pages, alas.

I think I've mentioned occurrences whenever I've noticed them.

Whilst I remember: LEGEND elements (even empty ones) are required within
FIELDSET elements, and they must be the first non-whitespace content.

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 22 May 2005 22:59:15, seen in
Michael Winter said:
On 22/05/2005 21:50, Dr John Stockton wrote:
I take it that you mean that TestBox.value should be
document.forms["xxx"].TestBox.value ?

That, or use an id with getElementById (and appropriate emulation for IE4).

DF = document.forms["xxx"]
....
with (DF) { ... }
seems the best fix; I name my elements so that "with" ambiguities are
unlikely.

Whilst I remember: LEGEND elements (even empty ones) are required within
FIELDSET elements, and they must be the first non-whitespace content.

Thanks. Now fixed in all the footers in the master, by substituting DIV
and adjusting TOE. I won't upload that change specifically, presuming
that the error was of little or no effect. I may change the style TOE
some more, as I don't like the result in IE4 (I suspect a bug). Short
changed page dm034-24.htm will be uploaded tonight, though; as will the
three pages which had that flaw in their "meat".
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top