jQuery:如何用反斜杠转义单引号和双引号 [英] jQuery: How to escape single and double quotes with backslash

查看:382
本文介绍了jQuery:如何用反斜杠转义单引号和双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一行而不是两行中用反斜杠转义单引号和双引号.

I want to escape single and double quotes with a backslash in one line rather than two.

单引号示例:

str = str.replace(/'/g, "\\'");

是否可以同时对双引号进行处理?

Is there a way to do this at the same time for double quotes included?

Sniffer在下面很好地回答了这个问题,我最终如下转义了我们需要的所有字符:

Sniffer Answered this very well below, and I ended up escaping all characters we needed as follows:

str = str.replace(/(['"&:;])/g, "\\$1");

再次感谢嗅探器的快速响应!

Thanks again Sniffer for the quick response!

推荐答案

尝试一下:

str = str.replace(/(['"])/g, "\\$1");

这篇关于jQuery:如何用反斜杠转义单引号和双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆