I would like a RegExp that will remove all special characters from a string. I am trying something like this but it doesn’t work in IE7, though it works in Firefox.
A detailed description of the RegExp would be helpful as well.
Toto 86.9k61 gold badges86 silver badges120 bronze badges asked Dec 7, 2010 at 8:47
Timothy RuhleTimothy Ruhle 7,12710 gold badges40 silver badges64 bronze badges 10
As was mentioned in the comments it's easier to do this as a whitelist - replace the characters which aren't in your safelist. The caret ( answered Dec 7, 2010 at 8:55
annakataannakata 73.1k16 gold badges112 silver badges180 bronze badges 9 Note that if you still want to exclude a set, including things like slashes and special characters you can do the following:
take special note that in order to also include the "minus" character, you need to escape it with a backslash like the latter group. if you don't it will also select 0-9 which is probably undesired. answered Jun 18, 2012 at 20:10
5 Plain Javascript regex does not handle Unicode letters.Do not use You really don't want remove these letters together with all the special characters. You have two chances:
answered Nov 27, 2016 at 17:25
freedevfreedev 22.4k7 gold badges102 silver badges117 bronze badges 3 The first solution does not work for any UTF-8 alphabet. (It will cut text such as Їжак). I have managed to create a function which does not use RegExp and use good UTF-8 support in the JavaScript engine. The idea is simple if a symbol is equal in uppercase and lowercase it is a special character. The only exception is made for whitespace.
Update: Please note, that this solution works only for languages where there are small and capital letters. In languages like Chinese, this won't work. Update 2: I came to the original solution when I was working on a fuzzy search. If you also trying to remove special characters to implement search functionality, there is a better approach. Use any
transliteration library which will produce you string only from Latin characters and then the simple Regexp will do all magic of removing special characters. (This will work for Chinese also and you also will receive side benefits by making answered Oct 21, 2014 at 8:50
SeagullSeagull 3,1461 gold badge28 silver badges37 bronze badges 4 using It's better to use all special characters in regex and exclude them from given string
answered May 18, 2021 at 11:53
answered Jun 22, 2017 at 21:16
1 I use RegexBuddy for debbuging my regexes it has almost all languages very usefull. Than copy/paste for the targeted language. Terrific tool and not very expensive. So I copy/pasted your regex and your issue is that [,] are special characters in regex, so you need to escape them. So the regex should be : /!@#$^&%*()+=-[\x5B\x5D]\/{}|:<>?,./im answered Dec 7, 2010 at 8:54
millebiimillebii 1,2672 gold badges17 silver badges27 bronze badges 0 @Seagull anwser (https://stackoverflow.com/a/26482552/4556619) looks good but you get undefined string in result when there are some special (turkish) characters. See example below.
i slightly improve it and patch with undefined check.
answered Apr 14 at 10:17
Fuad AllFuad All 7468 silver badges13 bronze badges why dont you do something like:
to check if your input contain any special char answered Dec 7, 2010 at 8:57
AnDAnD 2,9508 gold badges34 silver badges58 bronze badges 2 Not the answer you're looking for? Browse other questions tagged javascript regex special-characters or ask your own question. |