javascript - split intercepts equal sign
代言
代言 2017-06-15 09:23:13
0
7
1237

var string="content=ABCDEFGHIJKLMN="
var ca = string.split("=");
ca[0]=content;
ca[1]=ABCDEFGHIJKLMN;
ca[2]="";
But I don't want to intercept the second equal sign (it needs to be retained), how should I deal with this?

代言
代言

reply all(7)
phpcn_u64

666

我想大声告诉你

Use regular expressions

var string="content=ABCDEFGHIJKLMN="
var ca = string.split(/=(?=.)/);
大家讲道理

You are not suitable for programming, kid. . .
Just get ca[1]+"=". Why do you have to get the original equal sign? The idea must be flexible. The word equivalent is very important for a programmer.

typecho

After reading your comments in each answer, I would guess that your intention is to try to find a method built into the programming language to implement this function. You only need to call it, such as calling a function or adding parameters.

Right?

But the problem is that the current language may not have such a built-in feature (because I am not very proficient in js myself, so I dare not make a conclusion), so you need to implement it yourself.

In fact, the functions built into the language are just pre-implemented by others. You insist on finding one, but you fall into a trap.

As @G_Koala_C saidSolving problems in a simple and intuitive way is the way to go.

三叔

If there are more than two = wouldn’t it be a trap? So I have to

str1 = ca[0];
ca.shift();
str2 = ca.join('=');

Personally, I like regular expressions, but you can also use indexof to find the first =. But this method also needs to determine whether the return value is -1.

巴扎黑
stringObject.split(separator,howmany)

separator required. A string or regular expression to split the stringObject from where specified by this parameter.
howmany Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length.

Usage source http://www.w3school.com.cn/js...

迷茫

I looked at the source code of the querystring module of node.js. According to the source code, it is processed like this:

var string="content=ABCDEFGHIJKLMN="
var kstr,vstr;
var idx=string.indexOf('=')
if(idx>=0){
 kstr=string.substr(0,idx)
vstr=string.substr(idx+1)
}else{
   kstr=string
  vstr=''
}
console.log(kstr,vstr)  //=>content ABCDEFGHIJKLMN=
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!