Javascript 和反斜杠替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2479309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript and backslashes replace
提问by Frankie
here is my string:
这是我的字符串:
var str = "This is my \string";
This is my code:
这是我的代码:
var replaced = str.replace("/\/", "\\");
I can't get my output to be:
我不能让我的输出是:
"This is my \string"
I have tried every combination I can think of for the regular expression and the replacement value.
我已经尝试了我能想到的正则表达式和替换值的所有组合。
Any help is appreciated!
任何帮助表示赞赏!
回答by thegajman
Got stumped by this for ages and all the answers kept insisting that the source string needs to already have escaped backslashes in it ... which isn't always the case.
多年来一直被这个困扰,所有的答案都坚持认为源字符串需要已经在其中转义反斜杠......情况并非总是如此。
Do this ..
做这个 ..
var replaced = str.replace(String.fromCharCode(92),String.fromCharCode(92,92));
回答by Quentin
The string doesn't contain a backslash, it contains the \sescape sequence.
该字符串不包含反斜杠,它包含\s转义序列。
var str = "This is my \string";
And if you want a regular expression, you should have a regular expression, not a string.
如果你想要一个正则表达式,你应该有一个正则表达式,而不是一个字符串。
var replaced = str.replace(/\/, "\\");
回答by Tesserex
The problem is that the \ in your first line isn't even recognized. It thinks the backslash is going to mark an escape sequence, but \s isn't an escape character, so it's ignored. Your var str is interpreted as just "This is my string". Try str.indexOf("\\")- you'll find it's -1, since there is no backslash at all. If you control the content of str, do what David says - add another \ to escape the first.
问题是您的第一行中的 \ 甚至无法识别。它认为反斜杠将标记转义序列,但 \s 不是转义字符,因此被忽略。您的 var str 被解释为只是“这是我的字符串”。尝试str.indexOf("\\")- 你会发现它是 -1,因为根本没有反斜杠。如果您控制 str 的内容,请按照 David 所说的去做 - 添加另一个 \ 以转义第一个。
回答by user3426099
Use this
用这个
str.replace(/(\s)/g,function(str.replace(/ /g,'something').replace(/\s/g,'\s').replace(/something/g,' ');
){return var str=' \s';
str.replace(/\s/g,'\s');
// return '\s\s'
str.replace(/ /g,'SpAcE').replace(/\s/g,'\s').replace(/SpAcE/g,' ');
// return ' \s'
==' '?' ':'\s'})
or
或者
str.split(String.fromCharCode(92)).join(String.fromCharCode(92,92))
'something' it may be a combination of characters that is not in string
'something' 它可能是不在字符串中的字符组合
var exFnStr1 = exFn.toString();
var exFnStr = "";
var quoteStarted = false;
for(i = 0; i < exFnStr1.length; i++) {
var iChar = exFnStr1.charAt(i);
var oChar = exFnStr1.charAt(i);
var currentCharCode = exFnStr1.charCodeAt(i);
if(quoteStarted) {
if(currentCharCode === 9) oChar = "tabChar";
if(currentCharCode === 10) oChar = "newlineChar";
}
//console.log(iChar+"->"+currentCharCode+"->"+oChar)
exFnStr += oChar;
if(currentCharCode === 34) {
if(quoteStarted) quoteStarted = false;
else quoteStarted = true;
}
}
console.log(exFnStr);
//TODO - replace values in the string
exFn = new Function('return ' + exFnStr)();
回答by user2939415
In case you have multiple instances or the backslash:
如果您有多个实例或反斜杠:
var replaced = str.replace((new RegExp("\s"),"\s");
回答by Visv M
If use case is to replace some values in the toString of a function, and convert the string back to a valid function.
如果用例是替换函数的 toString 中的某些值,并将字符串转换回有效函数。
##代码##回答by Graza
I haven't tried this, but the following shouldwork
我没有试过这个,但以下应该工作
##代码##Essentially you don't want to replace "\", you want to replace the character represented by the "\s" escape sequence.
本质上您不想替换“\”,而是要替换由“\s”转义序列表示的字符。
Unfortunately you're going to need to do this for every letter of the alphabet, every number, symbol, etc in order to cover all bases
不幸的是,您将需要对字母表中的每个字母、每个数字、符号等执行此操作,以涵盖所有基础

