Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is enclosed in single quotes Javascript

// '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)?

like image 873
user544079 Avatar asked Jan 21 '15 20:01

user544079


People also ask

Can string be enclosed in single quotes?

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.

How do you check if a string has a 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.

How do you escape a single quote in JavaScript?

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.

Why use single quotes instead of double quotes JavaScript?

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.


2 Answers

if(str[0] == "'" && str[str.length - 1] == "'"){
   // do something
}
like image 141
Daniel Robinson Avatar answered Oct 27 '22 11:10

Daniel Robinson


/^'.*'$/.test(str)

Regex for starts and ends with a single quote.

like image 37
Scimonster Avatar answered Oct 27 '22 11:10

Scimonster