Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > regex help: alphanumeric with optional dashes

Reply
Thread Tools

regex help: alphanumeric with optional dashes

 
 
enrique
Guest
Posts: n/a
 
      05-19-2005
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.

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-19-2005
"enrique" <> writes:

> 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
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
enrique
Guest
Posts: n/a
 
      05-19-2005
> 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[i]") +
"</strong>&quot; <em>" + eval("IsAccountNumber(tests[i])") +
"</em></p>");
}
</script>

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      05-19-2005
JRS: In article < .com>
, dated Thu, 19 May 2005 09:12:59, seen in news:comp.lang.javascript,
enrique <> posted :
>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
<> :-
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-19-2005
"enrique" <> writes:

> 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[i]") +
> "</strong>&quot; <em>" + eval("IsAccountNumber(tests[i])") +
> "</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[i] +
"</strong>&quot;<em>" + IsAccountNumber(tests[i]) +
"</em></p>");

Never use "eval"

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
enrique
Guest
Posts: n/a
 
      05-19-2005
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.

 
Reply With Quote
 
enrique
Guest
Posts: n/a
 
      05-20-2005
Fill me on the "eval" function and your discouraging its use, please.

Thanks again.

epp

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      05-20-2005
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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      05-20-2005
JRS: In article <Xns965CA8C73B70eejj99@194.109.133.242>, dated Fri, 20
May 2005 14:35:21, seen in news:comp.lang.javascript, Evertjan.
<> posted :
>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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      05-20-2005
On 20/05/2005 21:33, Dr John Stockton wrote:

> JRS: In article <Xns965CA8C73B70eejj99@194.109.133.242>, dated Fri, 20
> May 2005 14:35:21, seen in news:comp.lang.javascript, Evertjan.
> <> 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.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd dashes on my home page. Paul HTML 2 06-27-2006 03:08 PM
regex debugging: alphanumeric with dashes enrique Java 2 05-19-2005 03:27 AM
Missing Dashes. Patrick D. Rockwell Computer Support 3 03-25-2005 05:36 AM
can i create a xalan java extension function where the function name has dashes (-) in it ? _clb_ Chris Bedford Java 0 08-17-2003 12:57 AM
can i create a xalan java extension function where the function name has dashes (-) in it ? _clb_ Chris Bedford XML 0 08-17-2003 12:57 AM



Advertisments