我有这个字符串

'john smith~123 Street~Apt 4~New York~NY~12345'

使用JavaScript,最快的方法是什么

var name = "john smith";
var street= "123 Street";
//etc...

当前回答

嗯,最简单的方法是:

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

其他回答

你可以使用split来拆分文本。

作为替代,你也可以使用match,如下所示

var str = 'john smith~123 Street~Apt 4~New York~NY~12345'; 匹配= str.match(/[^~]+/g); console.log(匹配); document . write(匹配);

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

您需要研究JavaScript的substr或split,因为这不是一个真正适合jQuery的任务。

用这个代码——

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

}

你不需要jQuery。

var s = 'john smith~123 Street~Apt 4~New York~NY~12345'; Var字段= s.split(/~/); 变量名称=字段[0]; Var street = fields[1]; console.log(名称); console.log(街);

尝试使用纯Javascript

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

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