Javascript regexp: replacing $1 with f($1)

前端 未结 3 1067
余生分开走
余生分开走 2021-01-19 07:26

I have a regular expression, say /url.com\\/([A-Za-z]+)\\.html/, and I would like to replace it with new string $1: f($1), that is, with a constant

3条回答
  •  攒了一身酷
    2021-01-19 07:46

    When using String.replace, you can supply a callback function as the replacement parameter instead of a string and create your own, very custom return value.

    'foo'.replace(/bar/, function (str, p1, p2) {
        return /* some custom string */;
    });
    

提交回复
热议问题