简体   繁体   English

Javascript正则表达式将反斜杠添加到字符串的每个字符中

[英]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 &amp; 注意如何将& get替换为&amp;

 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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript正则表达式:反斜杠后忽略每个字母 - Javascript regex: ignore every letter after backslash Javascript 正则表达式 - 仅将一个字符添加到匹配的字符串 - Javascript Regex - add only a character to matched string 在javascript字符串中的每个特殊字符之前添加一个字符 - Add one character before every special character in a string in javascript 通过正则表达式在JavaScript中搜索反斜杠+字符串 - Searching for backslash + string in javascript via regex Regex Express将字符串中的每个反斜杠\\实例替换为\\ u - Regex Express to replace every instance of backslash \ with a \u in a string 如何在javascript中的字符串中的每个单词的开头添加一个字符? - How to add a character to the beginning of every word in a string in javascript? 使用 keyup 正则表达式 javascript 在字符串中添加字符 - add character inside string using on keyup regex javascript javascript正则表达式反斜杠问题 - javascript regex backslash issue 为什么我必须在 javascript 正则表达式上添加双反斜杠? - Why do I have to add double backslash on javascript regex? 正则表达式每次在 JavaScript 中的字符串中重复时查找字符串中字符的偶数出现 - Regex to find even occurrence of a character in a string every time it repeats in a string in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM