使用javascript从字符串中删除字符 [英] Remove character from string using javascript

查看:83
本文介绍了使用javascript从字符串中删除字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有逗号分隔的字符串

var test = 1,3,4,5,6,

我想使用java脚本删除此字符串中的特定字符

i want to remove particular character from this string using java script

任何人都可以建议我吗?

can anyone suggests me?

推荐答案

JavaScript字符串为您提供 replace 方法,该方法将第一个实例替换为字符串的参数或 RegEx ,如果是全局的,则替换所有实例。

JavaScript strings provide you with replace method which takes as a parameter a string of which the first instance is replaced or a RegEx, which if being global, replaces all instances.

示例:

var str = 'aba';
str.replace('a', ''); // results in 'ba'
str.replace(/a/g, ''); // results in 'b'

如果你警告str - 你会得到相同的原始字符串原因字符串是不可变的。
您需要将其分配回字符串:

If you alert str - you will get back the same original string cause strings are immutable. You will need to assign it back to the string :

str = str.replace('a', '');

这篇关于使用javascript从字符串中删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆