// '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)?
A double-quoted string can have single quotes without escaping them, conversely, a single-quoted string can have double quotes within it without having to escape them. Double quotes ( \" ) must escape a double quote and vice versa single quotes ( \' ) must escape a single quote.
The includes() method is used to perform a case-sensitive search to detect whether a string contains another string or not and returns a Boolean value.
Using the Escape Character ( \ ) We can use the backslash ( \ ) escape character to prevent JavaScript from interpreting a quote as the end of the string. The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.
In JavaScript, single (' ') and double (“ ”) quotes are frequently used for creating a string literal. Generally, there is no difference between using double or single quotes, as both of them represent a string in the end.
if(str[0] == "'" && str[str.length - 1] == "'"){
// do something
}
/^'.*'$/.test(str)
Regex for starts and ends with a single quote.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With