ChatGPT解决这个技术问题 Extra ChatGPT

How can I escape a single quote?

How can I escape a ' (single quote) in HTML?

This is where I'm trying to use it:

<input type='text' id='abc' value='hel'lo'>

The result for the above code is "hel" populated in the text box. I tried to replace ' with \', but this what I'm getting.

<input type='text' id='abc' value='hel\'lo'>

The result for the above code is "hel" populated in the text box.

How can I successfully escape the single quotes?

Hey Ravi, this is more of an html question. I retagged the question as HTML, but you might want to change that in your question.

n
neaumusic

You could use HTML entities:

' for '

" for "

...

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


Why should one use double quotes for attribute values?
@Pascal MARTIN: XML does also allow single quotes for attribute values. (See w3.org/TR/xml/#NT-AttValue)
@Gumbo : wooo ! I don't think I've ever seen any XML document that was using single-quotes arround attributes values ; so I didn't think this was actually valid ;; I just (once again) learnt something while answering a question ; so, thanks for those comments !
I know this is old, but I think it's worth noting that there is an HTML entity for ": &quot;
@Pascal, w3c validator shows single quotes are valid for attributes. And see stackoverflow.com/questions/2210430/…
B
Benjamin Manns

You can use &apos; (which is iffy in IE) or &#39; (which should work everywhere). For a comprehensive list, see the W3C HTML5 Named Character References or the HTML entities table on WebPlatform.org.


Wow, it's been awhile since I wrote this. Thanks for catching it - I've updated the links to two tables on W3C and WebPlatform.org.
These work "everywhere" except in VXML Expression attributes where further escaping is necessary since in VXML a String variable value is indicated as single quotes within the normal double quotes of the attribute definitions:
G
Gumbo

As you’re in the context of HTML, you need to use HTML to represent that character. And for HTML you need to use a numeric character reference &#39; (&#x27; hexadecimal):

<input type='text' id='abc' value='hel&#39;lo'>

but I didn't understand what do you mean by context of html ?
@coding_idiot Have a look at the different tokens a HTML parser may encounter during the parsing process. Each state has a different set of parsing rules that are triggered based on the input. Not every state allows character references. Now if you look at the attribute value (single quoted), you can see that a & denotes the begin of a character reference.
P
Peter Mortensen

Represent it as a text entity (ASCII 39):

<input type='text' id='abc' value='hel&#39;lo'>

r
road242

Probably the easiest way:

<input type='text' id='abc' value="hel'lo">

P
Peter Mortensen

If for some reason you cannot escape the apostrophe character and you can't change it into a HTML entity (as it was in my case for a specific Vue.js property) you can use replace to change it into different apostrophe character from the UTF-8 characters set, for instance:

ʼ - U+02BC
’ - U+2019

This is a valid way of tackling the problem and I do it regularly. I just paste in the character directly.
I would avoid this if at all possible, and try to fix the underlying issue. It can cause unexpected problems. For example, if the user meant to include a code sample, then having characters changed like this will cause confusion for the users that copy and paste the code sample.
F
Flimm

I personally find the named HTML entities easier to remember:

' for ' (known as single quote or apostrophe, Unicode character U+0027)

" for " (known as double quote or quotation mark, Unicode character U+0022)

Demo:

'
"


L
Luca

You could try using: &#145;


R
Ram Bharlia

use javascript inbuild functions escape and unescape

for example

var escapedData = escape("hel'lo");   
output = "%27hel%27lo%27" which can be used in the attribute.

again to read the value from the attr

var unescapedData = unescape("%27hel%27lo%27")
output = "'hel'lo'"

This will be helpful if you have huge json stringify data to be used in the attribute


escape is not unicode safe. The specification advises against its use.
escape is deprecated. Also, it's not the type of escaping that is needed here, we are trying to escape ' to an HTML entity like &apos; or &#39;, we are not trying to do percent-encoding, which has a different use-case.