Remove all special characters except space from a string using JavaScript
You should use the string replace function, with a single regex.
Assuming by special characters, you mean anything that's not letter, here is a solution:
const str = "abc's test#s";console.log(str.replace(/[^a-zA-Z ]/g, ""));
How to remove all special characters except numbers and space in a string using JavaScript?
searchString.replace(/[^a-z\d\s]+/gi, "");
removes all but letters, numbers and whitespace.
var s = 'keep%8$this part 3£$@plz £$% @£';
s.replace(/[^a-z\d\s]+/gi, "");
// "keep8this part 3plz "
Remove all special characters except for @ symbol from string in JavaScript
If you want to keep the @
when it is followed by a word char and keeping the W
is also ok and also remove the newlines, you could for example change the \s
to match spaces or tabs [ \t]
Add the @
to the negated character class and use an alternation specifying to only match the @
when it is not followed by a word character using a negative lookahead.
[^\w \t@]+|@(?!\w)
[^\w \t@]+
Match 1+ times any char except a word char, space or tab|
Or@(?!\w)
Match an @ not directly followed by a word char
Regex demo
In the replacement use an empty string.
Remove all special characters with RegExp
var desired = stringToReplace.replace(/[^\w\s]/gi, '')
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 (^
) character is the negation of the set [...]
, gi
say global and case-insensitive (the latter is a bit redundant but I wanted to mention it) and the safelist in this example is digits, word characters, underscores (\w
) and whitespace (\s
).
How to remove special characters like $, @, % from string in jquery
You should explore Regex.
Try this:
var str = 'The student have 100% of attendance in @school';str= str.replace(/[^\w\s]/gi, '')document.write(str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
How to remove all special Characters from a string except - . and space
Use either \s
or simply a space character as explained in the
Pattern
class javadoc
\s - A whitespace character: [ \t\n\x0B\f\r]
- Literal space character
You must either escape -
character as \-
so it won't be interpreted as range expression or make sure it stays as the last regex character. Putting it all together:
filename.replaceAll("[^a-zA-Z0-9\\s.-]", "")
filename.replaceAll("[^a-zA-Z0-9 .-]", "")
Remove special symbols and extra spaces and replace with underscore using the replace method
Your regular expression [^a-zA-Z0-9]\s/g
says match any character that is not a number or letter followed by a space.
Remove the \s and you should get what you are after if you want a _ for every special character.
var newString = str.replace(/[^A-Z0-9]/ig, "_");
That will result in hello_world___hello_universe
If you want it to be single underscores use a + to match multiple
var newString = str.replace(/[^A-Z0-9]+/ig, "_");
That will result in hello_world_hello_universe
Remove all the characters and special characters except underscores, dashes and numbers
This is way easier with a negated character class:
str.replace(/[^0-9_-]/g, '');
Everything that is not a digit between 0 and 9, an underscore or a minus, will get replaced by an empty string.
(The first -
means “range” here, because it is between two other characters, the second one just means “itself”, because it is at the end of the character class. If it was placed somewhere other than the very start or end, it would need to be escaped, \-
.)
remove all special characters and replace space javascript
You can use regex to achieve it
replace(/[^a-zA-Z0-9 ]/g, "")
is to remove all special characters but keep space characters
replace(/ /g, "-")
is to replace all space characters with -
const str = "SanDisk SSD PLUS 1TB Internal SSD SATA III 6 Gb/s";
const result = str.replace(/[^a-zA-Z0-9 ]/g, "").replace(/ /g, "-")
console.log(result);
Related Topics
How to Remove Element from an Array in JavaScript
Calling Setstate in a Loop Only Updates State 1 Time
How to Stop a Page from Unloading (Navigating Away) in Js
Var Name Produces Strange Result in JavaScript
How to Clone a JavaScript Object Except for One Key
Fastest Way to Check a String Contain Another Substring in JavaScript
Sum of Array Object Property Values in New Array of Objects in JavaScript
Classical Inheritance VS Prototypal Inheritance in JavaScript
Node.Js Tail-Call Optimization: Possible or Not
How to Print a Stack Trace in Node.Js
How to Check Whether an Object Is a Date
How to Call Fromlatlngtodivpixel in Google Maps API V3
Convert Js Object to Form Data
React Router Doesn't Work in Aws S3 Bucket
Invariant Violation: _Registercomponent(...): Target Container Is Not a Dom Element