简体   繁体   English

JS 正则表达式字符串不会以句点/点结尾

[英]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:有效字符串:

  • .string 。细绳
  • ._string 。_细绳
  • string细绳
  • stri.ng细绳
  • st_ring细绳
  • st._ring细绳
  • st_ri_ng细绳

Invalid string:无效的字符串:

  • string.细绳。
  • _string. _细绳。
  • stri.ng.细绳。
  • st_ring.细绳。
  • st._ring.细绳。
  • st_ri_ng.细绳。

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 "."如果没有,您可以只读取最后一个字符并将其与"."进行比较。 . .

It would be much more efficient to use the endsWith method:使用endsWith方法会更有效:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM