简体   繁体   中英

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 is used after in an if statement and that's why I need to remove double quotes.

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 :)

EDIT: without 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("5439==5436 || 5439==5438")

But this assumes you have done some regex to convert your expression to something javascript can interpret.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM