ctr*_*yan 515 javascript string-split
我有这个字符串
'john smith~123 Street~Apt 4~New York~NY~12345'
Run Code Online (Sandbox Code Playgroud)
使用JavaScript,解析它的最快方法是什么
var name = "john smith";
var street= "123 Street";
//etc...
Run Code Online (Sandbox Code Playgroud)
Zac*_*ach 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.
Run Code Online (Sandbox Code Playgroud)
Gra*_*ner 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];
Run Code Online (Sandbox Code Playgroud)
Vah*_*aji 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
Run Code Online (Sandbox Code Playgroud)
您可能在输入字符串中有额外的项目.在这种情况下,您可以使用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"]
Run Code Online (Sandbox Code Playgroud)
我认为值的只读参考并使用了const
声明.
享受ES6!
Tor*_*ter 16
尽管这不是最简单的方法,但您可以这样做:
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"
}
Run Code Online (Sandbox Code Playgroud)
使用解构更新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
Run Code Online (Sandbox Code Playgroud)
split()
JavaScript 中的方法用于将字符串转换为数组。\n它采用一个可选参数(作为要分割的字符)。在你的情况下(〜)。
如果 splitOn 被跳过,它只会将字符串放在数组的第 0 个位置上。
\n如果 splitOn 只是 \xe2\x80\x9c\xe2\x80\x9d,那么它将转换单个字符数组。
\n所以在你的情况下:
\nvar arr = input.split(\'~\');\n
Run Code Online (Sandbox Code Playgroud)\n将获得名称arr[0]
和街道arr[1]
。
您可以阅读\n Split on in JavaScript以获取更详细的说明
\n好吧,最简单的方法是这样的:
var address = theEncodedString.split(/~/)
var name = address[0], street = address[1]
Run Code Online (Sandbox Code Playgroud)
如果Spliter发现则只
拆分它
else返回相同的字符串
Run Code Online (Sandbox Code Playgroud)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]; } } }
您可以使用split
拆分文本。
作为替代方案,您也可以使用match
如下
var str = 'john smith~123 Street~Apt 4~New York~NY~12345';
matches = str.match(/[^~]+/g);
console.log(matches);
document.write(matches);
Run Code Online (Sandbox Code Playgroud)
正则表达式[^~]+
将匹配除~
数组之外的所有字符并返回匹配项。然后,您可以从中提取匹配项。
归档时间: |
|
查看次数: |
808728 次 |
最近记录: |