Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regExp check last character

I'm using this RexExp code var userIdPattern = new RegExp('^([A-z0-9_.]{4,15})$');, I want to check if last character was dot (.) then .test() returns me false:

var userIdPattern = new RegExp('^([A-z0-9_.]{4,15})$');
console.log(userIdPattern.test('Omid.my.')); // -> I need this to be false

and in this case return me true:

userIdPattern.test('Omid.my'); //-> true
like image 895
Omid Avatar asked Feb 24 '26 00:02

Omid


1 Answers

Following the update, a more appropriate regex might be:

var userIdPattern = new RegExp('^([A-Za-z0-9\[\]\\^`][A-z0-9_.]{2,13}[A-Za-z0-9\[\]\\^`])$');

That is, if you want to include other special characters in the usernames like 7stud mentioned in his comment and only exclude . and _ from the first and last characters.

Otherwise, to prevent those characters, I would suggest:

var userIdPattern = new RegExp('^([A-Za-z0-9][A-Za-z0-9_.]{2,13}[A-Za-z0-9])$');

Fiddle to test.

like image 135
Jerry Avatar answered Feb 25 '26 18:02

Jerry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!