[英]Javascript regex to add to every character of a string a backslash
So as the title says I'd like to add to every character of a string a backslash, whether the string has special characters or not. 因此,正如标题所述,无论字符串是否包含特殊字符,我都想在字符串的每个字符中添加反斜杠。 The string should not be considered 'safe'
该字符串不应被视为“安全”
eg:
let str = 'dj%^3&something';
str = str.replace(x, y);
// str = '\d\j\%\^\3\&\s\o\m\e\t\h\i\n\g'
You could capture every character in the string with (.)
and use \\\\$1
as replacement, I'm not an expert but basically \\\\
will render to \\
and $1
will render to whatever (.)
captures. 您可以使用
(.)
捕获字符串中的每个字符,并使用\\\\$1
作为替换,我不是专家,但是基本上\\\\
将呈现为\\
而$1
将呈现为任何(.)
捕获。
HIH HIH
EDIT 编辑
please refer to Wiktor Stribiżew's comment for an alternative which will require less coding. 请参阅WiktorStribiżew的注释 ,以获取需要较少编码的替代方法。 Changes as follows:
更改如下:
str = str.replace(/(.)/g, '\\\\$1');
for str = str.replace(/./g, '\\\\$&');
为
str = str.replace(/./g, '\\\\$&');
Also, for future reference I strongly advice you to visit regexr.com when it comes to regular expressions, it's helped ME a lot 另外,作为正则参考,我强烈建议您访问regexr.com时使用正则表达式,这对我有很大帮助
let str = 'dj%^3&something'; str = str.replace(/(.)/g, '\\\\$1'); console.log(str);
If you just want to display a string safely, you should just do: 如果只想安全地显示字符串,则应该执行以下操作:
let str = 'dj%^3&something';
let node = document.createTextNode(str);
let dest = document.querySelector('.whatever');
dest.appendChild(node);
And then you are guaranteed that it will be treated as text, and won't be able to execute a script or anything. 然后,可以确保将其视为文本,并且将无法执行脚本或其他任何操作。
For example: https://jsfiddle.net/s6udj03L/1/ 例如: https : //jsfiddle.net/s6udj03L/1/
You can split the string to an array, add a \\
to each element to the array, then joint the array back to the string that you wanted. 您可以将字符串拆分为一个数组,在该数组的每个元素中添加一个
\\
,然后将该数组连接回所需的字符串。
var str = 'dj%^3&something'; var split = str.split(""); // split string into array for (var i = 0; i < split.length; i++) { split[i] = '\\\\' + split[i]; // add backslash to each element in array } var joint = split.join('') // joint array to string console.log(joint);
If you don't care about creating a new string
and don't really have to use a regex, why not just iterate over the existing one and place a \\
before each char
. 如果你不关心创建一个新的
string
,并不真的要使用正则表达式,为什么不遍历现有和地方\\
每个前char
。 Notice to you have to put \\\\
to escape the first \\
. 请注意,您必须输入
\\\\
以逃避第一个\\
。
To consider it safe, you have to encode it somehow. 为了安全起见,您必须以某种方式对其进行编码。 You could replace typical 'non-safe' characters like in the
encode()
function below. 您可以像下面的
encode()
函数中那样替换典型的“非安全”字符。 Notice how &
get's replaced by &
注意如何将
&
get替换为&
let str = 'dj%^3&something'; let out = ""; for(var i = 0; i < str.length; i++) { out += ("\\\\" + str[i]); } console.log(out); console.log(encode(out)); function encode(string) { return String(string).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.