Home  >  Q&A  >  body text

javascript - split intercepts equal sign

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?

代言代言2685 days ago1244

reply all(7)I'll reply

  • phpcn_u64

    phpcn_u642018-06-21 21:11:04

    666

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-15 09:25:13

    Use regular expressions

    var string="content=ABCDEFGHIJKLMN="
    var ca = string.split(/=(?=.)/);

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-15 09:25:13

    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.

    reply
    0
  • typecho

    typecho2017-06-15 09:25:13

    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.

    reply
    0
  • 三叔

    三叔2017-06-15 09:25:13

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-15 09:25:13

    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...

    reply
    0
  • 迷茫

    迷茫2017-06-15 09:25:13

    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=

    reply
    0
  • Cancelreply