Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all double quotes to single quotes using jquery?

I need to replace all double quotes to single quotes using jquery.

How I will do that.

I tested with this code but its not working properly.

newTemp = newTemp.mystring.replace(/"/g, "'");

like image 493
DEVOPS Avatar asked Nov 21 '11 04:11

DEVOPS


People also ask

How do you replace a double quote in a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How can I replace single quotes with double quotes in SQL Server?

Backspace over the double quotes and then type a single quote.

Should I use single or double quotes in 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.

What is the difference between single quotation and double quotation in JavaScript?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!


2 Answers

Use double quote to enclose the quote or escape it.

newTemp = mystring.replace(/"/g, "'"); 

or

newTemp = mystring.replace(/"/g, '\''); 
like image 79
amit_g Avatar answered Sep 18 '22 10:09

amit_g


You can also use replaceAll(search, replaceWith) [MDN].

Then, make sure you have a string by wrapping one type of quotes by a different type:

 'a "b" c'.replaceAll('"', "'")  // result: "a 'b' c"       'a "b" c'.replaceAll(`"`, `'`)  // result: "a 'b' c"   // Using RegEx. You MUST use a global RegEx(Meaning it'll match all occurrences).  'a "b" c'.replaceAll(/\"/g, "'")  // result: "a 'b' c" 

Important(!) if you choose regex:

when using a regexp you have to set the global ("g") flag; otherwise, it will throw a TypeError: "replaceAll must be called with a global RegExp".

like image 25
Raz Avatar answered Sep 20 '22 10:09

Raz