admin管理员组文章数量:1026989
How to remove everything but:
letters, numbers, spaces, exclamation marks, question marks from a string?
It's important that the method supports international languages (UTF-8).
How to remove everything but:
letters, numbers, spaces, exclamation marks, question marks from a string?
It's important that the method supports international languages (UTF-8).
Share Improve this question edited Apr 26, 2016 at 13:08 Sam Hanley 4,7557 gold badges37 silver badges64 bronze badges asked Sep 9, 2012 at 22:17 Tom SmykowskiTom Smykowski 26.1k57 gold badges165 silver badges247 bronze badges 1 |4 Answers
Reset to default 58You can use regex
myString.replace(/[^\w\s!?]/g,'');
This will replace everything but a word character, space, exclamation mark, or question.
Character Class:
\w
stands for "word character", usually[A-Za-z0-9_]
. Notice the inclusion of the underscore and digits.
\s
stands for "whitespace character". It includes[ \t\r\n]
.
If you don't want the underscore, you can use just [A-Za-z0-9]
.
myString.replace(/[^A-Za-z0-9\s!?]/g,'');
For unicode characters, you can add something like \u0000-\u0080
to the expression. That will exclude all characters within that unicode range. You'll have to specify the range for the characters you don't want removed. You can see all the codes on Unicode Map. Just add in the characters you want kept or a range of characters.
For example:
myString.replace(/[^A-Za-z0-9\s!?\u0000-\u0080\u0082]/g,'');
This will allow all the previously mentioned characters, the range from \u0000-\u0080
and \u0082
. It will remove \u0081
.
Both answers posted so far left out the question mark. I would comment on them, but don't have enough rep yet.
David is correct, sachleen's regex will leave underscores behind. rcdmk's regex, modified as follows, will do the trick, although if you care about international characters things might get a lot more complicated.
var result = text.replace(/[^a-zA-Z0-9\s!?]+/g, '');
This will leave behind new lines and tabs as well as spaces. If you want to get rid of new lines and tabs as well, change it to:
var result = text.replace(/[^a-zA-Z0-9 !?]+/g, '');
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"
You can try with a regular expression like: var cleaned = someString.replace(/[^a-zA-Z0-9! ]+/g, "");
How to remove everything but:
letters, numbers, spaces, exclamation marks, question marks from a string?
It's important that the method supports international languages (UTF-8).
How to remove everything but:
letters, numbers, spaces, exclamation marks, question marks from a string?
It's important that the method supports international languages (UTF-8).
Share Improve this question edited Apr 26, 2016 at 13:08 Sam Hanley 4,7557 gold badges37 silver badges64 bronze badges asked Sep 9, 2012 at 22:17 Tom SmykowskiTom Smykowski 26.1k57 gold badges165 silver badges247 bronze badges 1-
1
You want the expression to permit, or remove, characters such as:
ã
,é
andè
(and so on...)? – David Thomas Commented Sep 9, 2012 at 22:28
4 Answers
Reset to default 58You can use regex
myString.replace(/[^\w\s!?]/g,'');
This will replace everything but a word character, space, exclamation mark, or question.
Character Class:
\w
stands for "word character", usually[A-Za-z0-9_]
. Notice the inclusion of the underscore and digits.
\s
stands for "whitespace character". It includes[ \t\r\n]
.
If you don't want the underscore, you can use just [A-Za-z0-9]
.
myString.replace(/[^A-Za-z0-9\s!?]/g,'');
For unicode characters, you can add something like \u0000-\u0080
to the expression. That will exclude all characters within that unicode range. You'll have to specify the range for the characters you don't want removed. You can see all the codes on Unicode Map. Just add in the characters you want kept or a range of characters.
For example:
myString.replace(/[^A-Za-z0-9\s!?\u0000-\u0080\u0082]/g,'');
This will allow all the previously mentioned characters, the range from \u0000-\u0080
and \u0082
. It will remove \u0081
.
Both answers posted so far left out the question mark. I would comment on them, but don't have enough rep yet.
David is correct, sachleen's regex will leave underscores behind. rcdmk's regex, modified as follows, will do the trick, although if you care about international characters things might get a lot more complicated.
var result = text.replace(/[^a-zA-Z0-9\s!?]+/g, '');
This will leave behind new lines and tabs as well as spaces. If you want to get rid of new lines and tabs as well, change it to:
var result = text.replace(/[^a-zA-Z0-9 !?]+/g, '');
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"
You can try with a regular expression like: var cleaned = someString.replace(/[^a-zA-Z0-9! ]+/g, "");
本文标签:
版权声明:本文标题:javascript - How to remove everything but letters, numbers, space, exclamation and question mark from string? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1737233611a1451333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ã
,é
andè
(and so on...)? – David Thomas Commented Sep 9, 2012 at 22:28