我有一个POST AJAX命令,它返回以下内容:
`email{admin@stackoverflow.com} cid{215}`
我想做的是使用仅作为vars的值替换电子邮件{}和cid {}
var email = 'admin@stackoverflow.com'
var customer_id = 215;
他们会像那样。有没有比以前更清洁的方式:
var result = "email{admin@stackoverflow.com} cid{215}";
// change to admin@stackoverflow.com cid{215}
var replace1 = result.replace("email{");
var replace1a = replace1.replace("}");
// change to admin@stackoverflow.com 215
var replace2 = result.replace("cid{");
var replace2a = replace1.replace("}");
// now we have an email, with a space and a number
// admin@stackoverflow.com 215 make before space string
// this would be email
// now make only the int a string called cid
答案 0 :(得分:2)
首先使用正则表达式提取所需数据:
var response = "email{admin@stackoverflow.com} cid{215}";
var regex = /email\{(.*)\} cid\{(.*)\}/;
var data = response.match(regex);
现在您可以轻松获得所需的值:
var email = data[1];
var customer_id = +data[2];