转义引号-还有其他可以使用的字符吗?

发布于 2024-08-22 16:45:04 字数 311 浏览 2 评论 0原文

我了解 javascript 的转义引号等。是否有任何其他字符可以用来封装整个内容,而不仅仅是“或'?这两个字符都会出现在我的文本片段中,我不想在每个实例之前添加一个/

所以,而不是:

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

类似这样的东西(虽然我不希望使用百分号)

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

希望这有点道理,谢谢!

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 text where he said "I like this, she say's%

Hope that makes a bit of sense! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稳稳的幸福 2024-08-29 16:45:04

文字字符串需要使用 "' 进行声明。在这样的声明中,需要对字符串值周围的引号进行转义。因此:

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

两者都会被解释作为:

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

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
停顿的约定 2024-08-29 16:45:04

不。只有 "' 是有效的字符串分隔符。您必须将它们转义为

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

(作为作弊,您可以将字符串放入隐藏的 < div> 并读取 JS 中的 textContent。)

(作为另一个作弊,/Some text 他说“我喜欢这个,她说是/.source。)

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.)

烟燃烟灭 2024-08-29 16:45:04

如果您正在使用来自服务器端模板语言的内容,您可能还需要担心多字节序列和“控制”字符的编码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文