[英]get value of string before and after / in javascript
suppose i have 10 localstorage like blow: 假设我有10个本地存储,例如:
localStorage.setItem("item01","this is a value/100"); localStorage.setItem("item02","this new is a value/110"); localStorage.setItem("item03","this is a good value/120"); localStorage.setItem("item04","this is a nice value/130");
I need a java script code to check if the key
of for example item01
is not 0 then put the data
of item01
before /
to xyz
and the data after /
to rfv
variable. 我需要一个Java脚本代码来检查例如item01
的key
是否不为0,然后将item01
的data
item01
/
到xyz
之前,并将数据rfv
/
到rfv
变量之后。
I would suppose you split the data-string along the character "/". 我想您将数据字符串沿字符“ /”分开。 Something like: 就像是:
var lsData = localStorage.getItem("item01");
var dataArray = lsData.split("/");
if(dataArray.length === 2){
var xyz = dataArray[0];
var rfv = dataArray[1];
}
else{
... error-code (could not split exactly)...
}
This is an answer to your question "get value of string before and after / in javascript". 这是对您的问题“在javascript中/之前和之后获取字符串的值”的答案。 Like I stated in the comments you can split a string into an array of substrings using a delimiter. 就像我在评论中所述,您可以使用定界符将字符串拆分为子字符串数组。
var str= "this is a value/100";
var temp=str.split("/");
var xyz = temp[0]; //->this is a value
var rfv = temp[1]; //->100
alert('xyz = '+xyz+'\nrfv = '+rfv);
this should do it... 这应该做...
I'm assuming you know how to get the localStorage value, so I'm just posting an example. 我假设您知道如何获取localStorage的值,所以我只发布一个示例。
var a = "this is a value/100"
var b = /^(.*?)\/(\w+)/.exec(a);
var text = b[1]
var value = b[2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.