[英]javascript for checking alphabets from a string
I have a string which is of format 245545g65. 我有一个格式为245545g65的字符串。
var value = "245545g65"
var last3Letters = value.substring(7,9); // abc
Now I want to validate whether the last three letters contains only alphabets, if it is alphabet , i want to alert it.how to alert g? 现在我要验证最后三个字母是否仅包含字母,如果是Alphabet,我想提醒它。如何提醒g?
how do i do this? 我该怎么做呢?
assuming that "contains only alphabets" means the last three characters are a combination of the letters az: 假设“仅包含字母”表示最后三个字符是字母az的组合:
var str = '245545g65';
if (/[a-z]{3}$/.test(str)){
// last three characters are any combinations of the letters a-z
alert('Only letters at the end!');
}
you can use isNaN
to check weather s string is number 您可以使用isNaN
检查天气的字符串是否为数字
if (!isNan(last3Letters))
alert(last3Letters + ' is number.')
else
alert(last3Letters + ' is not number.')
Easy: 简单:
var alpha = /^[A-z]+$/;
alpha.test(last3Letters);
This will return a boolean (true/false). 这将返回一个布尔值(true / false)。 Stolen from here . 从这里被盗 。
you can use RegEx and compare length 您可以使用RegEx并比较长度
var re = new RegExp("[^0-9]*", "g");
var newlast3Letters =last3Letters.replace(re,"");
if(newlast3Letters.length!=last3Letters.length)
{
alert("not all alphabets");
}
else
{
alert("all alphabets");
}
You can also do this: 您也可以这样做:
var value = "245545g65"
if(value.slice(value.length-3).search(/[^a-z]/) < 0) {
alert("Just alphabets");
} else {
alert("Not just alphabets");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.