Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if string contains both a Single-quote (') and a double-quote (")? [closed]

Tags:

javascript

How can I check if string contains both a Single-quote (') and a double-quote ("), like the one below?

var str = "test'\"";
like image 849
Tree Avatar asked Feb 08 '11 16:02

Tree


People also ask

Which is a valid string that contains both single quotes and double quotes?

Enclosing Strings Containing Single and Double Quotes Actually, we can use triple quotes (i.e., triplet of single quotes or triplet double quotes) to represent the strings containing both single and double quotes to eliminate the need of escaping any. >>> print('''She said, "Thank you! It's mine."''')

How do you check if a string has a double quote?

charAt(test. length()-1) == 34) { System. out. println("Has double quotes"); } 34 in ascii table represents doublequote , hence it works fine.

What is the difference between single quote (') and double quote )?

The use of single quotes [ ' ... ' ] or double quotes [ “...” ] differs with context and geographic location. Conventionally, most English speaking countries use double quotes to mark direct speech and single quotes to mark speech within speech.

When declaring a string Is there a difference between single and double quotes?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.


2 Answers

A quick way to check if the string contains both a single quote and a double quote.

if (str.indexOf('\'') >= 0 && str.indexOf('"') >= 0) {
   //do something
}

edit: if the character is in the first position, indexOf will return zero.

like image 134
kmfk Avatar answered Sep 19 '22 18:09

kmfk


I'm guessing you want something like /['||"]/.test(str);

like image 22
scrappedcola Avatar answered Sep 20 '22 18:09

scrappedcola