Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   regex help: alphanumeric with optional dashes (http://www.velocityreviews.com/forums/t918171-regex-help-alphanumeric-with-optional-dashes.html)

enrique 05-19-2005 04:12 PM

regex help: alphanumeric with optional dashes
 
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.


Lasse Reichstein Nielsen 05-19-2005 06:44 PM

Re: regex help: alphanumeric with optional dashes
 
"enrique" <enrique.pineda@gmail.com> 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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

enrique 05-19-2005 07:07 PM

Re: regex help: alphanumeric with optional 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 :)


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>


Dr John Stockton 05-19-2005 07:11 PM

Re: regex help: alphanumeric with optional dashes
 
JRS: In article <1116519179.858023.320840@g43g2000cwa.googlegroups .com>
, dated Thu, 19 May 2005 09:12:59, seen in news:comp.lang.javascript,
enrique <enrique.pineda@gmail.com> 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
<lnwtuhfy7d.fsf@nuthaus.mib.org> :-
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.

Lasse Reichstein Nielsen 05-19-2005 07:32 PM

Re: regex help: alphanumeric with optional dashes
 
"enrique" <enrique.pineda@gmail.com> 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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

enrique 05-19-2005 07:56 PM

Re: regex help: alphanumeric with optional dashes
 
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.


enrique 05-20-2005 02:27 PM

Re: regex help: alphanumeric with optional dashes
 
Fill me on the "eval" function and your discouraging its use, please.

Thanks again.

epp


Evertjan. 05-20-2005 02:35 PM

Re: regex help: alphanumeric with optional dashes
 
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)


Dr John Stockton 05-20-2005 08:33 PM

Re: regex help: alphanumeric with optional dashes
 
JRS: In article <Xns965CA8C73B70eejj99@194.109.133.242>, dated Fri, 20
May 2005 14:35:21, seen in news:comp.lang.javascript, Evertjan.
<exjxw.hannivoort@interxnl.net> 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.

Michael Winter 05-20-2005 11:12 PM

Re: regex help: alphanumeric with optional dashes
 
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.
> <exjxw.hannivoort@interxnl.net> 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.


All times are GMT. The time now is 04:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.