如何从字符串中删除字符

How to remove a character from a string

本文关键字:删除 字符 字符串      更新时间:2023-09-26

我需要删除url中的#,例如我有字符串:

message:  hello http://www.google.it#readme

并且我必须删除"#"字符。这是我在node.js:中的代码

messagge.replace(new RegExp(/((http|https)'S*#'S*)+/g),function(x){
                    x.replace('#','');
                    console.log(x);
            });

控制台打印链接,但链接未更改为:http://www.google.it#readme.有人可以帮忙找到解决方案吗?

将替换项分配给x:

messagge.replace(new RegExp(/((http|https)'S*#'S*)+/g),function(x){
                    x = x.replace(new RegExp(/#/g),'');
                    console.log(x);
            });

这样使用它可以消除链接中的所有"#"。

x = x.replace(/'#/g,'');

或者像这样只消除一个:

x = x.replace('#','');