I need to change @x ( x a number) to x. How can I do that, I don't know js regex..
-
Answers where already posted and I can't comment on the question, but here is a link to regex in javascript if you want to learn more about it.MSclavi– MSclavi2019-04-14 03:20:25 +00:00Commented Apr 14, 2019 at 3:20
-
1It's pretty straight forward, you can try to learn something about regex here give it a try if you still face any problem comment here will be happy to helpCode Maniac– Code Maniac2019-04-14 03:23:18 +00:00Commented Apr 14, 2019 at 3:23
Add a comment
|
3 Answers
You can use inbuilt replace function for this purpose, which can take both, literals and regex pattern as parameter.
var str = "@12345";
str.replace("@", "");
We can also use patterns in the replace parameter, if you have multiple values to be replaced.
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
gstands for global search- [@#] stands for either
@or#, you can add anything here - \D stands for anything other than digits