I have an essay as a big string with letters and numbers (as page numbers) (only single digit and double digits). I want all of them to be replaced by "#". I tried str.replace(/[0-9]/g,"#"), this worked for 0 to 9 numbers, but with the double digits, they were replaced by "##", so how can I replace them all with a single "#"?
Asked
Active
Viewed 1,302 times
-4
-
How do you know if it is a double digit number or two single digit ones?? – Yunfei Chen Jul 08 '20 at 17:51
-
Use `[0-9]+`..... – Jul 08 '20 at 17:52
-
Do you only want to replace one or two digits? What about 3 or more? – Code-Apprentice Jul 08 '20 at 17:53
-
yes the numbers are up to 99 and I want to replace all of them with a single "#" – ganesh tarone Jul 08 '20 at 17:54
-
Can you do a second pass replacing ## with #? – outis Jul 08 '20 at 17:53
1 Answers
2
If you want to replace only 1 or 2 digits in a line:
str.replace(/[0-9]{1,2}/, "#")
If you want to replace all digits in a line:
str.replace(/[0-9]+/g, "#")

sit
- 360
- 3
- 9
-
This will replace strings of digits regardless of the length with "#". The OP is asking about how to replace only 1 or 2 digits. – Code-Apprentice Jul 08 '20 at 17:52
-
1