Reputation:
know how to match one pattern using jaascipt:
var pattern = somePattern
if (!pattern.test(email)) { //do something }
but what if I have to match string start with " and end with "; and anything in between with a specific length(10) so if I have this:
"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 ";
How can I achieve this using js?
Upvotes: 2
Views: 231
Reputation: 2573
Just in case you want a solution other than regex, then the following function can also test if the passed string matches your criteria or not..
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;
}
Upvotes: 1
Reputation: 67968
(?=^".{10}";$)^".*";$
You can use postive lookahead to check the length.See demo.
http://regex101.com/r/oC9iD0/1
Upvotes: 0