Deniss Muntjans
Deniss Muntjans

Reputation: 379

JS Regex string don't end up with a period/dot

I would like to have a validation for a particular string but it can't end up with a period/dot, how could you achieve that?

Valid string:

Invalid string:

Upvotes: 0

Views: 799

Answers (2)

Lord Elrond
Lord Elrond

Reputation: 16072

It would be much simpler to use the endsWith method:

var validString = '.string';
var invalidString = 'string.';

validString.endsWith('.'); //false
invalidString.endsWith('.'); //true

Upvotes: 2

Robo Robok
Robo Robok

Reputation: 22845

The pattern you are looking for is /[^.]$/.

However, I must ask: does it need to be regex? If not, you can just read the last character and compare it to ".".

Upvotes: 3

Related Questions