我有这样一个字符串
'john smith~123 Street~Apt 4~New York~NY~12345'
使用JavaScript,有什么最快的方法可以将其解析为
var name = "john smith";
var street= "123 Street";
//etc...
用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.
根据ECMAScript6ES6
,干净的方法是破坏数组。
<!--开始片段。 js hide: false console: true babel.true --> -- begin snippet: js hide: false console: true true -->
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 运算符来获取其余项的数组,或者直接忽略它们。
-- begin snippet: js hide: false console: truetrue -->
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!
虽然这不是最简单的方法,但你可以这样做。
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
如果发现分裂者,则仅有
分开来
否则返回相同的字符串
符号化的字符串 {
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",如下所示。
<!--开始片段。 js hide.false --> -- begin snippet: js hide: false -->
var str = 'john smith~123 Street~Apt 4~New York~NY~12345';
matches = str.match(/[^~]+/g);
console.log(matches);
document.write(matches);
<!--结束片段-->
regex [^~]+
将匹配除~
以外的所有字符,并在一个数组中返回匹配结果。
然后你可以从中提取出匹配的字符。
扎克说的没错... 使用他的方法,你也可以做一个看似"多维" 数组... 我在JSFiddle上创建了一个快速的例子
。// array[0][0] will produce brian
// array[0][1] will produce james
// array[1][0] will produce kevin
// array[1][1] will produce haley
var array = [];
array[0] = "brian,james,doug".split(",");
array[1] = "kevin,haley,steph".split(",");
这个string.split("~")[0];
就能搞定。
源。 [String.prototype.split()][1] 。
另一种使用库里和函数组成的功能方法.。
所以第一件事就是拆分函数。
我们要把这个"john smith~123 Street~Apt 4~New York~NY~12345"
变成这个["john smith", "123 Street", "Apt 4", "New York", "NY", "12345"]
。
const split = (separator) => (text) => text.split(separator);
const splitByTilde = split('~');
所以现在我们可以使用我们专门的splitByTilde
函数。
例如:
splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // ["john smith", "123 Street", "Apt 4", "New York", "NY", "12345"]
splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // ["john smith", "123 Street", "Apt 4", "New York", "NY", "12345"]
为了得到第一个元素,我们可以使用list[0]
运算符。
让我们建立一个first
函数。
const first = (list) => list[0];
算法是。
用冒号分割,然后得到列表中的第一个元素。
所以我们可以将这些函数进行编译,建立我们最后的getName
函数。
用reduce
构建一个compose
函数。
const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value);
现在用它来编写splitByTilde
和first
函数。
const getName = compose(first, splitByTilde);
let string = 'john smith~123 Street~Apt 4~New York~NY~12345';
getName(string); // "john smith"
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
使用此代码 -- --
function myFunction() {
var str = "How are you doing today?";
var res = str.split("/");
}
JavaScript。 转换字符串为数组的JavaScript拆分
<!--开始片段。 js hide: false console: true babel.false --> -- begin snippet: js hide: false console: true false -->
var str = "This-javascript-tutorial-string-split-method-examples-tutsmake."
var result = str.split('-');
console.log(result);
document.getElementById("show").innerHTML = result;
<html>
<head>
<title>How do you split a string, breaking at a particular character in javascript?</title>
</head>
<body>
<p id="show"></p>
</body>
</html>
<!--结束片段-->
https://www.tutsmake.com/javascript-convert-string-to-array-javascript/