how can i remove chars between indexes in a javascript string

前端 未结 10 2450
无人及你
无人及你 2020-12-17 09:39

i have the following:

var S=\"hi how are you\";
var bindex = 2;
var eindex = 6;

how can i remove all the chars from S that reside between

10条回答
  •  没有蜡笔的小新
    2020-12-17 10:25

    A solution that doesn't require creating any intermediate arrays or strings is to use .replace to capture the first characters in a group, match the characters you want to remove, and replace with the first captured group:

    // keep first 3 characters, remove next 4 characters
    const s = "hi how are you";
    console.log(
      s.replace(/(.{3}).{4}/, '$1')
    );

提交回复
热议问题