/    Sign up×
Community /Pin to ProfileBookmark

Regular Expression Error

I don’t know why my ‘re.test’ gives different result each times.

>

re = /^([0-9A-Za-z-_])*$/g;
/^([0-9A-Za-z-
_
])*$/g
> re.test(“v3”)
true
> re.test(“v3”)
false
> re.test(“v3”)
true
> re.test(“v3”)
false
> re.test(“v3”)
true
> re.test(“v3”)
false

I know the re show be /^([0-9A-Za-z-_])*$/, but the result is weird.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 18.2010 — What in fact are you testing for? And please, post the [I]exact[/I] code you are using.
Copy linkTweet thisAlerts:
@Gozzy82Mar 18.2010 — i tried it here: http://www.webdevout.net/test?08

i also do not understand why the global modifier makes this regexp behave so strange, would like to know too! ?

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;Test&lt;/p&gt;
&lt;script type="text/javascript"&gt;
// make regular expression

var re = /^([0-9A-Za-z-_])*/g
for(var i = 0; i &lt; 10; i++){
document.write(re.test("v3") + "&lt;br /&gt;");
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @Daniel_Cai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 11.14,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @aldoushuxley,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,
)...