string - JavaScript - replace and create vars -
i have post ajax command returns following:
`email{admin@stackoverflow.com} cid{215}` what want replace email{} , cid{} using values vars
var email = 'admin@stackoverflow.com' var customer_id = 215; they appear that. there cleaner way than:
var result = "email{admin@stackoverflow.com} cid{215}"; // change admin@stackoverflow.com cid{215} var replace1 = result.replace("email{"); var replace1a = replace1.replace("}"); // change admin@stackoverflow.com 215 var replace2 = result.replace("cid{"); var replace2a = replace1.replace("}"); // have email, space , number // admin@stackoverflow.com 215 make before space string // email // make int string called cid
first use regular expression extract desired data:
var response = "email{admin@stackoverflow.com} cid{215}"; var regex = /email\{(.*)\} cid\{(.*)\}/; var data = response.match(regex); now can obtain values want:
var email = data[1]; var customer_id = +data[2];
Comments
Post a Comment