我有这个字符串

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

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

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

当前回答

尝试使用纯Javascript

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

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

其他回答

如果分裂者被发现,那么只有

它会分裂

否则返回相同的字符串

函数 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 address = theEncodedString.split(/~/)
var name = address[0], street = address[1]

因为分隔逗号的问题被复制到这个问题上,所以在这里添加这个。

如果你想对一个字符进行拆分,并处理该字符后面可能出现的额外空格,通常使用逗号,你可以使用替换然后拆分,如下所示:

var items = string.replace(/,\s+/, ",").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("=");