[英]Javascript Regex Replace - Not replacing all the required strings
I have no idea what is happening here.. 我不知道这里发生了什么。
model.attributes.data.Path.replace('/\\/g',""), @options.path.replace('/\\/g',"")
When doing : 进行时:
console.log model.attributes.data.Path.replace('/\\/g',""),
@options.path.replace('/\\/g',"")
the data is: 数据是:
T2/T2_2, T2/T2_2 T2 / T2_2,T2 / T2_2
It returns this: 它返回以下内容:
T2T2_2, T2/T2_2 T2T2_2,T2 / T2_2
So only the first path was replaced, but not the second one? 因此,仅替换了第一条路径,而没有替换第二条路径? Why would that be? 为什么会这样呢?
Aside from the fact you're matching backslashes ( \\\\
= \\
), instead of forward slashes ( \\/
= /
), Don't put your regexes into the replace function as strings. 除了要匹配反斜杠( \\\\
= \\
)而不是正斜杠( \\/
= /
)之外,不要将正则表达式作为字符串放入替换函数中。
Use: 采用:
.replace(/\//g,"");
Instead of 代替
.replace('/\//g',"");
Then it'll work just fine: 然后就可以了:
"T2/T2_2 , T2/T2_2".replace(/\//g,"");
// returns: "T2T2_2 , T2T2_2"
Otherwise, it'll just try to literally find the string '/\\//g'
. 否则,它将尝试从字面上找到字符串 '/\\//g'
。
Also, to replace both forward and backslashes in 1 regex, try this: 另外,要在1个正则表达式中同时替换正斜杠和反斜杠,请尝试以下操作:
"T2/T2_2 , T2\T2_2".replace(/\/|\\/g,"");
// returns: "T2T2_2 , T2T2_2"
# \/|\\ Matches:
# \/ - Forward slash
# | - Or
# \\ - Backslash
Try: 尝试:
model.attributes.data.Path.replace(/\//g,"")
@options.path.replace(/\//g,"")
/\\\\/g
matches a backslash and /\\//g
matches a forward slash. /\\\\/g
匹配一个反斜杠,而/\\//g
匹配一个正斜杠。
尝试使用.replace(/\\//g,"")
代替.replace('/\\\\/g',"")
(正则表达式不是字符串)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.