使用Javascript替换正则表达式

使用Javascript替换正则表达式,javascript,Javascript,我需要把@x(x是一个数字)改成x。 我怎么做呢,我不知道js regex..你可以这样试试 var n=Number(s.replace(/\D+/,'') 只需使用替换,如下所示: 常量str=“@1235”; const num=str.replace(“@”和“); console.log(num)为此,您可以使用内置的replace函数,该函数可以将文本和正则表达式模式作为参数 var str = "@12345"; str.replace("@", ""); 如果要替换多个值,

我需要把@x(x是一个数字)改成x。 我怎么做呢,我不知道js regex..

你可以这样试试

var n=Number(s.replace(/\D+/,'')


只需使用
替换
,如下所示:


常量str=“@1235”;
const num=str.replace(“@”和“);

console.log(num)为此,您可以使用内置的
replace
函数,该函数可以将文本和正则表达式模式作为参数

var str = "@12345";
str.replace("@", "");
如果要替换多个值,我们还可以在replace参数中使用模式

var str = "@123#45";
str.replace(/[@#]/,"")     // prints "123#45" => removes firs occurrence only
str.replace(/[@#]/g,"")    // prints "12345"
str.replace(/\D/,"")       // prints "123#45"  => removes any non-digit, first occurrence
str.replace(/\D/g,"")      // prints "12345"  => removes any non-digit, all occurrence
  • g
    代表全局搜索
  • [@#]代表
    @
    ,您可以在此处添加任何内容
  • \D代表数字以外的任何东西

回答了已经发布的问题,我不能对这个问题发表评论,但是如果您想了解更多,请使用javascript访问regex。这很简单,您可以尝试了解regex的一些信息。如果您仍然面临任何问题,请尝试一下。这里的评论将很乐意提供帮助
var str = "@123#45";
str.replace(/[@#]/,"")     // prints "123#45" => removes firs occurrence only
str.replace(/[@#]/g,"")    // prints "12345"
str.replace(/\D/,"")       // prints "123#45"  => removes any non-digit, first occurrence
str.replace(/\D/g,"")      // prints "12345"  => removes any non-digit, all occurrence