Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to escape quotes in JavaScript

Tags:

People also ask

How do you escape quotes in JavaScript?

Javascript uses '\' (backslash) in front as an escape character. To print quotes, using escape characters we have two options: For single quotes: \' (backslash followed by single quote) For double quotes: \” (backslash followed by double quotes)

How do you escape quotes in a string?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.

How do you escape a double 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.

What is escape function in JavaScript?

JavaScript escape() Function Use encodeURI() or encodeURIComponent() instead. The escape() function encodes a string. This function makes a string portable, so it can be transmitted across any network to any computer that supports ASCII characters.


to start off, I don't do much JavaScript and am a complete newbie at it, now that's out of the way.. I've got a slight problem I'm trying to escape quotes from users inputs in my search app:

function getQString()
{
var query_str = 'q=' + $('input[name=q]').val().trim();
return  query_str;
}

This is done as a method within a gsp file, is there something equivalent to .escape() in JavaScript?

This query is later sent to elastic search and gives me hell due to the quotes especially input like a"b..

I'm using ES 0.20.6