如何使用Javascript从字符串中删除字符?

use*_*504 311 javascript string replace character

我非常接近这个,但这是不对的.我想做的就是从字符串中删除字符"r".问题是,字符串中有多个"r"实例.但是,它始终是索引4处的字符(因此是第5个字符).

示例字符串:"crt/r2002_2"我想要的:"crt/2002_2"

这个替换功能删除了"r"

mystring.replace(/r/g, '')
Run Code Online (Sandbox Code Playgroud)

产生:"ct/2002_2"

我试过这个功能:

String.prototype.replaceAt = function (index, char) {
    return this.substr(0, index) + char + this.substr(index + char.length);
}
mystring.replaceAt(4, '')
Run Code Online (Sandbox Code Playgroud)

它只有在我用另一个角色替换它时才有效.它不会简单地删除它.

有什么想法吗?

JKi*_*rtz 417

var mystring = "crt/r2002_2";
mystring = mystring.replace('/r','/');
Run Code Online (Sandbox Code Playgroud)

将取代/r/使用String.prototype.replace.

或者,您可以使用带有全局标记的正则表达式(如下面的Erik ReppenSagar Gala所建议的)来替换所有出现的内容

mystring = mystring.replace(/\/r/g, '/');
Run Code Online (Sandbox Code Playgroud)

  • 第四个字符不一定是"/" (3认同)

小智 60

一个简单的功能JavaScript方式将是

mystring = mystring.split('/r').join('/')

简单,快速,全局替换,无需功能或原型

  • 这对我很有效.当我尝试mystring = mystring.replace('/ r','/'); 它只转换第一个校正器,其余的都是原样. (3认同)
  • 第四个字符不一定是'/' (2认同)

sim*_*igh 47

总是有字符串函数,如果你知道你总是要删除第四个字符:

str.slice(0, 4) + str.slice(5, str.length))
Run Code Online (Sandbox Code Playgroud)

  • @TekTimmy 为什么不呢?`str.slice(0, 0) + str.slice(1, str.length)` 删除第一个和 `str.slice(0, str.length-1) + str.slice(str.length, str.length) )` 删除最后一个。(当然,如果你知道你总是删除第一个或最后一个,你会删除多余的部分,但如果它们是变量,它们就像任何其他索引一样工作。) (4认同)
  • 末尾多了一个`)`。 (2认同)

Eri*_*pen 26

你的第一个功能几乎是正确的.只需删除代表'global'(编辑)的'g'标志,并给它一些上下文来发现第二个'r'.

编辑:没有看到它是第二个'r'之前添加'/'.当使用regEx arg时,需要\ /以逃避'/'.感谢upvotes,但我错了,所以我会修复并为有兴趣了解regEx基础知识的人添加更多细节,但这样可行:

mystring.replace(/\/r/, '/')
Run Code Online (Sandbox Code Playgroud)

现在进行过多的解释:

在读取/写入regEx模式时,请考虑以下方面:<a字符或字符集>后跟<a字符或字符集>后跟<...

在regEx中,<a字符或字符集>可以是一次一个:

/each char in this pattern/
Run Code Online (Sandbox Code Playgroud)

所以读作e,接着是a,然后是c等......

或者单个<a字符或一组charcters>可以是字符类描述的字符:

/[123!y]/
//any one of these
/[^123!y]/
//anything but one of the chars following '^' (very useful/performance enhancing btw)
Run Code Online (Sandbox Code Playgroud)

或者扩展以匹配一定数量的字符(但仍然最好将其视为顺序模式中的单个元素):

/a{2}/
//precisely two 'a' chars - matches identically as /aa/ would

/[aA]{1,3}/
//1-3 matches of 'a' or 'A'

/[a-zA-Z]+/
//one or more matches of any letter in the alphabet upper and lower
//'-' denotes a sequence in a character class

/[0-9]*/
//0 to any number of matches of any decimal character (/\d*/ would also work)
Run Code Online (Sandbox Code Playgroud)

所以一起喝酒:

   var rePattern = /[aA]{4,8}(Eat at Joes|Joes all you can eat)[0-5]+/g
   var joesStr = 'aaaAAAaaEat at Joes123454321 or maybe aAaAJoes all you can   eat098765';

   joesStr.match(rePattern);

   //returns ["aaaAAAaaEat at Joes123454321", "aAaAJoes all you can eat0"]
   //without the 'g' after the closing '/' it would just stop at the first   match and return:
   //["aaaAAAaaEat at Joes123454321"]
Run Code Online (Sandbox Code Playgroud)

当然,我已经过度阐述,但我的观点仅仅是这个:

/cat/
Run Code Online (Sandbox Code Playgroud)

是一系列3个模式元素(后面是事物后面的东西).

这是这样的:

/[aA]{4,8}(Eat at Joes|Joes all you can eat)[0-5]+/
Run Code Online (Sandbox Code Playgroud)

由于regEx开始看起来很古怪,所以这一切都分解为一系列事物(可能是多角色的事物)依次相继跟随.这是一个基本点,但是我花了一段时间才过去,所以我已经过分解释它,因为我认为这将有助于OP和其他新的regEx了解正在发生的事情.读取/写入regEx的关键是将其分解为这些部分.


Sag*_*ala 13

对于'/ r'的全局替换,此代码对我有用.

mystring = mystring.replace(/\/r/g,'');
Run Code Online (Sandbox Code Playgroud)


Ken*_*ite 7

只需修复您的replaceAt

String.prototype.replaceAt = function(index, charcount) {
  return this.substr(0, index) + this.substr(index + charcount);
}

mystring.replaceAt(4, 1);
Run Code Online (Sandbox Code Playgroud)

removeAt取而代之。:)


Kam*_*ski 7

这是simpleigh 答案的改进(省略长度)

s.slice(0, 4) + s.slice(5)
Run Code Online (Sandbox Code Playgroud)

s.slice(0, 4) + s.slice(5)
Run Code Online (Sandbox Code Playgroud)


Naz*_*riy 6

let str = '1234567';
let index = 3;
str = str.substring(0, index) + str.substring(index + 1);
console.log(str) // 123567 - number "4" under index "3" is removed
Run Code Online (Sandbox Code Playgroud)


Mat*_*t S 6

也许我是个菜鸟,但我今天遇到了这些,它们看起来都不必要地复杂。

这是一种(对我来说)更简单的方法,可以从字符串中删除您想要的任何内容。

function removeForbiddenCharacters(input) {
let forbiddenChars = ['/', '?', '&','=','.','"']

for (let char of forbiddenChars){
    input = input.split(char).join('');
}
return input
Run Code Online (Sandbox Code Playgroud)

}


Eug*_*sky 5

return this.substr(0, index) + char + this.substr(index + char.length);
Run Code Online (Sandbox Code Playgroud)

char.length是零。1在这种情况下,您需要添加才能跳过字符。


Raj*_*jiv 5

创建如下函数

  String.prototype.replaceAt = function (index, char) {
      if(char=='') {
          return this.slice(0,index)+this.substr(index+1 + char.length);
      } else {
          return this.substr(0, index) + char + this.substr(index + char.length);
      }
  }
Run Code Online (Sandbox Code Playgroud)

替换如下给出的字符

  var a="12346";
  a.replaceAt(4,'5');
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

并删除确定索引处的字符,将第二个参数指定为空字符串

a.replaceAt(4,'');
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明