How to Escape a Single Quote

How can I escape a single quote?

You could use HTML entities:

  • ' for '
  • " for "
  • ...

For more, you can take a look at Character entity references in HTML.

How to escape single quotes within single quoted strings

If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

 alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234

Explanation of how '"'"' is interpreted as just ':

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.

How do I escape a single quote ( ' ) in JavaScript?

You should always consider what the browser will see by the end. In this case, it will see this:

<img src='something' onmouseover='change(' ex1')' />

In other words, the "onmouseover" attribute is just change(, and there's another "attribute" called ex1')' with no value.

The truth is, HTML does not use \ for an escape character. But it does recognise " and ' as escaped quote and apostrophe, respectively.

Armed with this knowledge, use this:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change("ex1")' />";

... That being said, you could just use JavaScript quotes:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";

How do I escape a single quote in SQL Server?

Single quotes are escaped by doubling them up, just as you've shown us in your example. The following SQL illustrates this functionality. I tested it on SQL Server 2008:

DECLARE @my_table TABLE (
[value] VARCHAR(200)
)

INSERT INTO @my_table VALUES ('hi, my name''s tim.')

SELECT * FROM @my_table

Results

value
==================
hi, my name's tim.

Escape single quote within double quote within single quote

A simple advice: when in doubt and no special needs that force you to use both single and double quotes, just normalize the quotes and escape the inner ones:

curl -X POST --data-urlencode "payload={\"channel\": \"@somebody\", \"text\": \"I'm sending you a test message. Let me know if you get it.\"}" https://hooks.slack.com/services/XXX

It's cleaner and intuitive.

If you want to really escape the single quote inside double quotes:

 curl -X POST --data-urlencode 'payload={"channel": "@somebody", "text": "I'\''m sending you a test message. Let me know if you get it."}' https://hooks.slack.com/services/XXX

How to escape single quote in sed?

Quote sed codes with double quotes:

    $ sed "s/ones/one's/"<<<"ones thing"   
one's thing

I don't like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way:

    $ sed 's/ones/one\x27s/'<<<"ones thing"
one's thing

How to use single quotes in gatsby frontmatter?

That front matter is in YAML, which is a can be a bit confusing in how it escapes characters depending on the style of string that is in use. In this case, we have single quoted strings. According to the docs, in a single quoted string you'd escape a single quote by doubling it.

So your example would be:

---
title: 'that''s great'
---

If you were to instead use a double quoted string, you would use \ to escape any characters in a more traditional way (and single quotes wouldn't need any escaping at all):

---
title: "that's great, but you now need to escape \"double\" quotes, as well as any \\ characters"
---

Bearing in mind you want to just use ' as an apostrophe in your strings, maybe changing to the double quoted style in the second example above would be your best bet. You don't need to modify your strings, and the only characters that need escaping are " and \.

YAML also has a load more ways of denoting strings, including ones that can be more preserve line breaks. You can read more in the documentation for YAML. In particular, look at 4.5. Scalar Styles



Related Topics



Leave a reply



Submit