/    Sign up×
Community /Pin to ProfileBookmark

Regular expression

I am trying to setup regular expression that allows only alphas, digits and if ‘_v’ is typed it should follow by a number and nothing else. I tried this:

[code=php]regex = new RegExp(“[a-zA-Z0-9]+’_v'[0-9]”, “i”);[/code]

But doesn’t seem to work.
Any help is appreciated,
webtekie

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@NedalsMar 17.2005 — Pretty close ?

The _v should not be quoted.

Did you only what 1 digit after the _
v? If so take out the + after d+ (equiv to [0-9])
<i>
</i>var data = "_v2788c2";
if (/^([a-zA-Z0-9]+|_vd+)$/.test(data)) { alert('ok') } else { alert('no good') }
Copy linkTweet thisAlerts:
@scragarMar 17.2005 — regex = new RegExp("([a-zA-Z0-9]|_v[0-9])", "i");
Copy linkTweet thisAlerts:
@A1ien51Mar 17.2005 — nevermind....
Copy linkTweet thisAlerts:
@webtekieauthorMar 17.2005 — thanks guys,

So, regex = new RegExp("([a-zA-Z0-9]|_v[0-9])", "i"); will check if I type something like test_vd23 regex will catch it and it I type test_v23 it will allow?
Copy linkTweet thisAlerts:
@NedalsMar 17.2005 — No!

regex = new RegExp("([a-zA-Z0-9]|_v[0-9])", "i");

Will build a regex but will not do anything by itself. In addition, if you use this regex it will pass just about anything.

You need to perform a TEST as I coded above!

Now that you have REDEFINED the problem, you wll need something like this
<i>
</i>var data = "test_v27882";
var regex = new RegExp("^[a-z0-9]+(_v[0-9]+)*$", "i");
if (regex.test(data)) { alert('ok') } else { alert('no good') }

OR

if (/^[a-z0-9]+(_vd+)*$/i.test(data)) { alert('ok') } else { alert('no good') }
Copy linkTweet thisAlerts:
@webtekieauthorMar 17.2005 — Sorry for confusion.

Works great!

thanks,

webtekie
×

Success!

Help @webtekie 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 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...