Reputation: 2924
I am new to regex and trying validation in javascript/jquery using regex, appreciate any help.
The input is :
Valid test cases:
Abc_def_gh_123
Abc_def_1h_103_BA
Abc_def_1h_103_BA
Invalid test cases
___
_ _ _
Abc_d
Abc_def_ghi_de21_
Abc_def_fg
Abc_def_fg_
_Abc_def_fg
I had tried regex in javascript
/^[a-zA-Z0-9]+_[a-zA-Z0-9]+_[a-zA-Z0-9]+_[a-zA-Z0-9]+$/
but it fails eg. if the string has more than 3 underscores
Upvotes: 2
Views: 155
Reputation: 67978
(?=^[a-zA-Z0-9]+?_[a-zA-Z0-9]+?_[a-zA-Z0-9]+?_[a-zA-Z0-9_]*$)(?!.*?_$)(?!.*?_{2,}.*)^.*$
Try this.This works.
See demo.
http://regex101.com/r/pP3pN1/5
Upvotes: 2
Reputation: 14882
/([a-zA-Z0-9]+_[a-zA-Z0-9]+){3,}$/mg
http://regex101.com/r/yQ2xS9/2
Breakdown:
[a-zA-Z0-9]+
Starts with any number of lowercase and uppercase letters, or numbers.
_[a-zA-Z0-9]+
Contains an underscore followed by another sequence of lowercase/uppercase letters and numbers.
{3,}$
Minimum of three times (three lots of underscores)
Happy coding :)
Upvotes: 1
Reputation: 82461
This regexp should do the job:
/^([a-zA-Z0-9]+_){3,}[a-zA-Z0-9]+$/
Upvotes: 6