开发者

Escaping quotes-are there other characters that can be used?

开发者 https://www.devze.com 2022-12-21 03:56 出处:网络
I understand escaping quotes etc. for javascript.Are there any other characters that can be used to encase the whole thing rather than just \" or \'?Both characters will come up in my piece of text an

I understand escaping quotes etc. for javascript. Are there any other characters that can be used to encase the whole thing rather than just " or '? Both characters will come up in my piece of text and I don't want to have to put a / before each instance

So, instead of:

("Some text where he said "I like this, she say's")

something like this(although I'm not expecting the percentage sign to be used)

(%Some tex开发者_如何转开发t where he said "I like this, she say's%

Hope that makes a bit of sense! Thank you.


Literal strings need to be declared with either " or '. And within such a declaration, the quotes that surround the string value need to be escaped. So:

"Some text where he said \"I like this, she say's"
'Some text where he said "I like this, she say\'s'

Both will be interpreted as:

Some text where he said "I like this, she say's


No. Only " and ' are valid string delimiters. You have to escape them either as

"...\"...'...";
'..."...\'...';

(As a cheat, you can put the string in a hidden <div> and read the textContent in JS.)

(As another cheat, /Some text where he said "I like this, she say's/.source.)


If you're working with content that comes from your server-side template language, you may also have to worry about encoding multi-byte sequences and "control" characters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号