How do I split a string, breaking at a particular character?
我有这串
1
| 'john smith~123 Street~Apt 4~New York~NY~12345' |
使用JavaScript,将其解析为最快的方法是
1 2 3
| var name ="john smith";
var street="123 Street";
//etc... |
使用JavaScript的String.prototype.split函数:
1 2 3 4 5 6 7
| 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。
1 2 3 4
| 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,干净的方法是破坏数组:
1 2 3 4 5 6 7 8 9 10
| 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运算符获取其余的数组,或者忽略它们:
1 2 3 4 5 6 7
| 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!
即使这不是最简单的方法,也可以执行以下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 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更新
1 2 3 4 5 6
| 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的任务。
如果找到分离器,则仅
拆分
否则返回相同的字符串
1 2 3 4 5 6 7 8 9 10
| 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];
}
}
} |
好吧,最简单的方法是:
1 2
| var address = theEncodedString.split(/~/)
var name = address[0], street = address[1] |
您可以使用split分割文本。
或者,您也可以按照以下方式使用match
1 2 3 4 5
| var str = 'john smith~123 Street~Apt 4~New York~NY~12345';
matches = str.match(/[^~]+/g);
console.log(matches);
document.write(matches); |
正则表达式[^~]+将匹配除~以外的所有字符,并在数组中返回匹配项。然后,您可以从中提取匹配项。
就像是:
1 2 3
| var divided = str.split("/~/");
var name=divided[0];
var street = divided[1]; |
可能是最简单的
Zach拥有这一权利..使用他的方法,您还可以制作一个看似"多维"的数组。.我在JSFiddle http://jsfiddle.net/LcnvJ/2/创建了一个快速示例
1 2 3 4 5 6 7 8 9
| // 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(","); |
尝试使用纯Javascript
1 2 3
| //basic url=http://localhost:58227/ExternalApproval.html?Status=1
var ar= [url,statu] = window.location.href.split("="); |
此string.split("~")[0];完成任务。
来源:String.prototype.split()
另一种使用咖喱和功能成分的功能方法。
因此,第一件事就是拆分功能。我们要将此"john smith~123 Street~Apt 4~New York~NY~12345"放入此["john smith","123 Street","Apt 4","New York","NY","12345"]
1 2
| const split = (separator) => (text) => text.split(separator);
const splitByTilde = split('~'); |
所以现在我们可以使用我们专用的splitByTilde函数。例:
1
| splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // ["john smith","123 Street","Apt 4","New York","NY","12345"] |
要获取第一个元素,我们可以使用list[0]运算符。让我们构建一个first函数:
1
| const first = (list) => list[0]; |
该算法是:用冒号分隔,然后获取给定列表的第一个元素。因此,我们可以组合这些函数来构建最终的getName函数。使用reduce构建compose函数:
1
| const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value); |
现在使用它来构成splitByTilde和first函数。
1 2 3 4
| const getName = compose(first, splitByTilde);
let string = 'john smith~123 Street~Apt 4~New York~NY~12345';
getName(string); //"john smith" |
JavaScript:将字符串转换为数组JavaScript拆分
1 2 3 4 5 6 7
| var str ="This-javascript-tutorial-string-split-method-examples-tutsmake."
var result = str.split('-');
console.log(result);
document.getElementById("show").innerHTML = result; |
1 2 3 4 5 6 7 8 9 10 11
| <html>
<head>
How do you split a string, breaking at a particular character in javascript?
</head>
<body>
<p id="show">
</p>
</body>
</html> |
JavaScript: Convert String to Array JavaScript
由于逗号分割问题与该问题重复,因此请在此处添加。
如果要拆分一个字符并处理该字符后可能出现的多余空格(通常在逗号中出现),可以使用replace然后split,如下所示:
1
| var items = string.replace(/,\s+/,",").split(',') |
使用此代码-
1 2 3 4 5
| function myFunction() {
var str ="How are you doing today?";
var res = str.split("/");
} |
|