[英]Removing double quotes from a string with javascript
I've got a string that I make dynamically.我有一个动态生成的字符串。 At the end I've got this:
最后我得到了这个:
fullcondition: "5439=5436 or 5439=5438"
The variable fullcondition contains "5439=5436 or 5439=5438"
with these double quotes.变量 fullcondition 包含带有这些双引号的
"5439=5436 or 5439=5438"
。
fullcondition is used after in an if statement and that's why I need to remove double quotes. fullcondition 在 if 语句之后使用,这就是我需要删除双引号的原因。
if ("5439=5436 or 5439=5438")
does not work What I need is:不起作用我需要的是:
if (5439=5436 or 5439=5438).
Thanks in advance.提前致谢。
You can write something like:你可以这样写:
var x = "5439=5436 or 5439=5438".replace(/=/g, '===').split(' or ');
if ( eval(x[0]) || eval(x[1]) ) {
// some code
}
but using eval can be dangerous :)但使用 eval 可能很危险:)
EDIT: without eval
:编辑:没有
eval
:
var x = "5439=5436 or 5439=5438".split(' or ');
var left = x[0].split('=');
var right = x[1].split('=');
if (left[0]===left[1] || right[0]===right[1]) {
// some code
}
or shorten:或缩短:
var x = "5439=5436 or 5439=5438".replace(' or ', '=').split('=');
if (x[0]===x[1] || x[2]===x[3]) {
// some code
}
You can use the eval to evaluate a string:您可以使用 eval 来评估字符串:
eval("5439==5436 || 5439==5438")
But this assumes you have done some regex to convert your expression to something javascript can interpret.但这假设您已经完成了一些正则表达式将您的表达式转换为 javascript 可以解释的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.