Answers:
这是您的PHP代码的直接转换:
//Loading the variable
var mystr = '0000000020C90037:TEMP:data';
//Splitting it with : as the separator
var myarr = mystr.split(":");
//Then read the values from the array where 0 is the first
//Since we skipped the first element in the array, we start at 1
var myvar = myarr[1] + ":" + myarr[2];
// Show the resulting value
console.log(myvar);
// 'TEMP:data'
split(delimiter,limit)
的limit参数与explode($delimiter,$string,$limit)
的limit参数不同。示例:explode('.','1.2.3.4',3) === array('1','2','3.4')
-使用Javascript时,您将获得:'1.2.3.4'.split('.',3) === ['1', '2', '3']
。任何人都知道如何轻松地复制PHP的方法吗?
String.prototype.explode = function (separator, limit)
{
const array = this.split(separator);
if (limit !== undefined && array.length >= limit)
{
array.push(array.splice(limit - 1).join(separator));
}
return array;
};
应该准确地模仿PHP的explode()函数。
'a'.explode('.', 2); // ['a']
'a.b'.explode('.', 2); // ['a', 'b']
'a.b.c'.explode('.', 2); // ['a', 'b.c']
尝试这个:
arr = str.split (":");
创建一个对象:
// create a data object to store the information below.
var data = new Object();
// this could be a suffix of a url string.
var string = "?id=5&first=John&last=Doe";
// this will now loop through the string and pull out key value pairs seperated
// by the & character as a combined string, in addition it passes up the ? mark
var pairs = string.substring(string.indexOf('?')+1).split('&');
for(var key in pairs)
{
var value = pairs[key].split("=");
data[value[0]] = value[1];
}
// creates this object
var data = {"id":"5", "first":"John", "last":"Doe"};
// you can then access the data like this
data.id = "5";
data.first = "John";
data.last = "Doe";
如果您喜欢php,请查看php.JS-JavaScript爆炸
或使用普通的JavaScript功能:
var vInputString = "0000000020C90037:TEMP:data";
var vArray = vInputString.split(":");
var vRes = vArray[1] + ":" + vArray[2]; `
emptyArray = { 0: '' }
?:-/
explode('foo','bar'); // 'bar'
这就是我到达此处的原因,出于我的目的,我打算使用带有限制的复制和粘贴脚本绝对完美。我不必为创建自己的游戏而烦恼。为什么还要重新发明轮子,亲亲
console.log(('0000000020C90037:TEMP:data').split(":").slice(1).join(':'))
输出: TEMP:data
无意批评John Hartsock,以防万一使用给定代码的分隔符的数量可能有所不同,我正式建议改用...
var mystr = '0000000020C90037:TEMP:data';
var myarr = mystr.split(":");
var arrlen = myarr.length;
var myvar = myarr[arrlen-2] + ":" + myarr[arrlen-1];
var str = '0000000020C90037:TEMP:data'; // str = "0000000020C90037:TEMP:data"
str = str.replace(/^[^:]+:/, ""); // str = "TEMP:data"
所以我知道这篇文章已经很老了,但是我认为我还可以添加多年来对我有帮助的功能。为什么不使用如上所述的split重新制作爆炸功能?好吧,这是:
function explode(str,begin,end)
{
t=str.split(begin);
t=t[1].split(end);
return t[0];
}
如果您要获取两个值之间的值,则此功能效果很好。例如:
data='[value]insertdataherethatyouwanttoget[/value]';
如果您有兴趣从两个“值”“标签”之间获取信息,则可以使用如下功能。
out=explode(data,'[value]','[/value]');
//Variable out would display the string: insertdataherethatyouwanttoget
但是,可以说您没有上面显示的示例那样的方便的“标签”。不管。
out=explode(data,'insert','wanttoget');
//Now out would display the string: dataherethatyou
想要看到它在行动吗?请点击这里。
var str = "helloword~this~is~me";
var exploded = str.splice(~);
exploded变量将返回数组,您可以访问该数组的元素,并对其进行访问true true exploded [nth]其中nth是您要获取的值的索引
我用过slice,split和join你可以只写一行代码
let arrys = (str.split(":").slice(1)).join(":");
如果要定义自己的函数,请尝试以下操作:
function explode (delimiter, string, limit) {
if (arguments.length < 2 ||
typeof delimiter === 'undefined' ||
typeof string === 'undefined') {
return null
}
if (delimiter === '' ||
delimiter === false ||
delimiter === null) {
return false
}
if (typeof delimiter === 'function' ||
typeof delimiter === 'object' ||
typeof string === 'function' ||
typeof string === 'object') {
return {
0: ''
}
}
if (delimiter === true) {
delimiter = '1'
}
// Here we go...
delimiter += ''
string += ''
var s = string.split(delimiter)
if (typeof limit === 'undefined') return s
// Support for limit
if (limit === 0) limit = 1
// Positive limit
if (limit > 0) {
if (limit >= s.length) {
return s
}
return s
.slice(0, limit - 1)
.concat([s.slice(limit - 1)
.join(delimiter)
])
}
// Negative limit
if (-limit >= s.length) {
return []
}
s.splice(s.length + limit)
return s
}