user4023870
user4023870

Reputation:

javascript to match string start with " and end with "; and anything in between?

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

Answers (3)

Abdul Jabbar
Abdul Jabbar

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;
}

Fiddle

Upvotes: 1

vks
vks

Reputation: 67968

(?=^".{10}";$)^".*";$

You can use postive lookahead to check the length.See demo.

http://regex101.com/r/oC9iD0/1

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

Use .{10} to match exactly 10 characters.

^".{10}";$

DEMO

Upvotes: 1

Related Questions