0

次を返すPOSTAJAXコマンドがあります。

`email{admin@stackoverflow.com} cid{215}`

私がしたいのは、email{}とcid{}をvarsとして値のみを使用するように置き換えることです

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
4

1 に答える 1

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];
于 2012-06-05T11:55:02.953 に答える