我有这个字符串
'john smith~123 Street~Apt 4~New York~NY~12345'
使用JavaScript,解析它的最快方法是什么
var name = "john smith"; var street= "123 Street"; //etc...
Zach.. 817
使用JavaScript的String.prototype.split
功能:
var input = 'john smith~123 Street~Apt 4~New York~NY~12345'; var fields = input.split('~'); var name = fields[0]; var street = fields[1]; // etc.
Grant Wagner.. 50
你不需要jQuery.
var s = 'john smith~123 Street~Apt 4~New York~NY~12345'; var fields = s.split(/~/); var name = fields[0]; var street = fields[1];
您不需要为这个简单的替换添加正则表达式.如果有的话,它只会让它变慢.您可以将其更改为引号以进行简单的字符串替换. (52认同)
谢谢你指出`split`接受正则表达式! (10认同)
Vahid Hallaj.. 40
根据ECMAScript6 ES6
,干净的方法是破坏数组:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, unit, city, state, zip] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(unit); // Apt 4
console.log(city); // New York
console.log(state); // NY
console.log(zip); // 12345
您可能在输入字符串中有额外的项目.在这种情况下,您可以使用rest运算符为其余运算符获取数组,或者只是忽略它们:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, ...others] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(others); // ["Apt 4", "New York", "NY", "12345"]
我认为值的只读参考并使用了const
声明.
享受ES6!
使用JavaScript的String.prototype.split
功能:
var input = 'john smith~123 Street~Apt 4~New York~NY~12345'; var fields = input.split('~'); var name = fields[0]; var street = fields[1]; // etc.
你不需要jQuery.
var s = 'john smith~123 Street~Apt 4~New York~NY~12345'; var fields = s.split(/~/); var name = fields[0]; var street = fields[1];
根据ECMAScript6 ES6
,干净的方法是破坏数组:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, unit, city, state, zip] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(unit); // Apt 4
console.log(city); // New York
console.log(state); // NY
console.log(zip); // 12345
尽管这不是最简单的方法,但您可以这样做:
var addressString = "~john smith~123 Street~Apt 4~New York~NY~12345~", keys = "name address1 address2 city state zipcode".split(" "), address = {}; // clean up the string with the first replace // "abuse" the second replace to map the keys to the matches addressString.replace(/^~|~$/g).replace(/[^~]+/g, function(match){ address[ keys.unshift() ] = match; }); // address will contain the mapped result address = { address1: "123 Street" address2: "Apt 4" city: "New York" name: "john smith" state: "NY" zipcode: "12345" }
使用解构更新ES2015
const [address1, address2, city, name, state, zipcode] = addressString.match(/[^~]+/g); // The variables defined above now contain the appropriate information: console.log(address1, address2, city, name, state, zipcode); // -> john smith 123 Street Apt 4 New York NY 12345
你会想要研究JavaScript的substr或split,因为这不是一个适合jQuery的任务
好吧,最简单的方法是这样的:
var address = theEncodedString.split(/~/) var name = address[0], street = address[1]
如果Spliter发现则只
拆分它
else返回相同的字符串
function SplitTheString(ResultStr) { if (ResultStr != null) { var SplitChars = '~'; if (ResultStr.indexOf(SplitChars) >= 0) { var DtlStr = ResultStr.split(SplitChars); var name = DtlStr[0]; var street = DtlStr[1]; } } }