知道如何使用jaascipt:匹配一个模式
var pattern = somePattern
if (!pattern.test(email)) { //do something }
但如果我必须匹配以"开头、以"结尾的字符串,该怎么办;以及介于两者之间的任何特定长度(10),所以如果我有这个:
"ww#="$==xx"; (acceptable)
"w#="$==xx"; (Not acceptable, the length is 9 between the " and the ";)
ww#="$==xx"; (Not acceptable), doesn't start with "
"ww#="$==xx" (Not acceptable), doesn't end with ";
如何使用js实现这一点?
使用.{10}
精确匹配10个字符。
^".{10}";$
演示
如果您想要regex以外的解决方案,那么下面的函数也可以测试传递的字符串是否符合您的条件。。
function matchString(str){
var result = "InCorrect";
if(str.length != 13)
result = "InCorrect";
else{
if(str[0] == '"' && str[str.length-1] == ';' && str[str.length-2] == '"')
result = "Correct";
}
return result;
}
Fiddle
(?=^".{10}";$)^".*";$
您可以使用正向前瞻来检查长度。请参阅演示。
http://regex101.com/r/oC9iD0/1