我有这个字符串

'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.

其他提示

你不需要 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

输入字符串中可能有额外的项目。在这种情况下,您可以使用剩余运算符来获取其余部分的数组或忽略它们:

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

你会想研究一下 JavaScript 子字符串 或者 分裂, ,因为这并不是真正适合 jQuery 的任务。

好吧,最简单的方法是这样的:

var address = theEncodedString.split(/~/)
var name = address[0], street = address[1]

如果 发现分离器 那么只有

拆分它

否则返回 相同的字符串

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];
        }
    }
}

就像是:

var divided = str.split("/~/");
var name=divided[0];
var street = divided[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);

正则表达式 [^~]+ 将匹配除以下字符之外的所有字符 ~ 并返回数组中的匹配项。然后您可以从中提取匹配项。

扎克说得对..使用他的方法,你还可以制作一个看似“多维”的数组。我在 JSFiddle 创建了一个快速示例 http://jsfiddle.net/LcnvJ/2/

// 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()


另一种使用柯里化和函数组合的函数方法。

所以第一件事就是 split 函数。我们想做这个 "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"]

要获取第一个元素,我们可以使用 list[0] 操作员。让我们构建一个 first 功能:

const first = (list) => list[0];

算法是:用冒号分割,然后获取给定列表的第一个元素。所以我们可以组合这些函数来构建我们的最终版本 getName 功能。建设一个 compose 功能与 reduce:

const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value);

现在用它来创作 splitByTildefirst 功能。

const getName = compose(first, splitByTilde);

let string = 'john smith~123 Street~Apt 4~New York~NY~12345';
getName(string); // "john smith"

由于逗号分割问题与此问题重复,因此在此处添加此问题。

如果您想分割一个字符并处理该字符后面可能出现的额外空格(这通常发生在逗号中),您可以使用 replace 然后 split, , 像这样:

var items = string.replace(/,\s+/, ",").split(',')

尝试使用纯 JavaScript

 //basic url=http://localhost:58227/ExternalApproval.html?Status=1

 var ar= [url,statu] = window.location.href.split("=");

使用这个代码——

function myFunction() {
var str = "How are you doing today?";
var res = str.split("/");

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top