So, I have a string something like, asd.asd.asd.234432$$..asd888
. Now I want to get a string like, .234432888
. So what I want to achieve is to remove every dots except for the first one and remove every non number character.
So far I tried *string*.replace(/[^d.]/gi, new String())
. But, it does not work as expected.
4
Answers
Do a global regex match of characters:
(?<!..*)
– find.
that doesn’t have.
before it at any distance (.*
) using a lookbehind negative assertion. That would be the first.
encountered.d+
– find all numbersYou can use
(?<=.[^.]*).|[^d.]+
regex and replace it with empty string.Here
(?<=.[^.]*).
part matches all dots except first dot and[^d.]+
matches any non-digit non-dot one or more character.Demo
JS code demo,
Possible and easier with the
string.match()
:Also, there’s a thing that you should be aware of: In
[]
, you can’t use special statements such asd
–[d]
is treated as „or
d
“. You need to use character range such as0-9
.