Reputation: 16629
// 'Apple' : valid
// 'Apple : Invalid
// Apple : Invalid
if(str.indexOf('\'') > -1 && str.indexOf('"') > -1){
// do something
}
This will detect if the string contains single and double quotes.
How to check if string starts and ends with a single quote. (Regexp)?
Upvotes: 8
Views: 12973
Reputation: 720
if (str.startsWith("'") && str.endsWith("'")) {
// do something
}
Not supported in IE
MDN startsWith endsWith
Upvotes: 1
Reputation: 1
if((name[0] == "'" && name[name.length - 1] == "'")||(name[0] == '"' && name[name.length - 1] == '"')){
///
}
I'd suggest this... It's a total plagiarism of Daniel Robinson's answer, just more complete.
Upvotes: 0
Reputation: 3387
if(str[0] == "'" && str[str.length - 1] == "'"){
// do something
}
Upvotes: 14
Reputation: 33409
/^'.*'$/.test(str)
Regex for starts and ends with a single quote.
Upvotes: 3
Reputation: 32202
No need for regex, you can use the charAt
function:
if (str.charAt(0) == "'" && str.charAt(str.length-1) == "'") {
}
Upvotes: 1