What does RegExp.$1 do

后端 未结 6 868
温柔的废话
温柔的废话 2021-02-02 13:34

I have come across a piece of code in JScript:

RegExp.$1

Does anybody know what it does?

If I output it on its own, I get nothing not e

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-02 13:58

    $1 is whatever is matched in the first capture. If you have more captures you can use $2, $3 etc.

    Ex:

    "abc".replace(/(.)/, "$1$1"); // aabc
    "abc".replace(/(.{2})/, "$1$1"); // ababc
    "abc".replace(/(.)(.)/, "$2$1"); // bac
    

提交回复
热议问题