About the sample code of input components in WeChat Mini Programs's document?

document link https://developers.weixin.qq.

there is a bindReplaceInput method in the sample code as follows. The purpose of this code is: if I enter two digits in a row, 1meme 11 will become 2

.
bindReplaceInput: function (e) {
     
    var value = e.detail.value 
    var pos = e.detail.cursor
     
    var left   
    if (pos !== -1) {
      // 
      left = e.detail.value.slice(0, pos);
      // 
      pos = left.replace(/11/g, "2").length; 
    }
 
    // 
    return {
      value: value.replace(/11/g, "2"),
      cursor: pos
    }
  }

my question is:

    In the
  1. code, there is an if (pos! = =-1). May I ask why the value of pos is compared to-1? It seems that the value of pos will not be equal to a negative number
  2. in the sentence segment of if, if the value of left does not contain 11, left.replace (/ 11 li) cannot replace 11 with 2 because 11 cannot be found, so why does this statement not report an error

novice, please give me a lot of advice and thanks!

Aug.07,2021

figured it out by myself, judging-1, may be for the rigor of the code; the last regular / 11max g will not report an error if the match is incorrect, and if the match fails, the original value will be returned, as shown in figure
https://img.mukewang.com/5bbc.

.
Menu