How to remove everything but:
letters, numbers, spaces, exclamation marks, question marks from a string?
It\'s important that the method supports international l
text = "A(B){C};:a.b*!c??!1<>2@#3"
result = text.replace(/[^a-zA-Z0-9]/g, '')
Should return ABCabc123
First, we define text as
A B C a b c 1 2 3
but with random characters set theresult
as:
text.replace(...)
where the parameters are:
/.../g
,/.../
:^
means to reverse; not to remove the letters which are:
a-z
(lowercase letters),A-Z
(UPPERCASE letters) and0-9
(digits)
g
means global, to remove all matches not just the first matchThe second parameter is the replacement character, we set it to an empty string so that it just keeps the specified string. if
is specified, it will return this:
"A B C a b c 1 2 3"