Use the global flag:
var name = name.replace(/[^a-zA-Z ]/g, "");
^
If you don't want to remove numbers, add it to the class:
var name = name.replace(/[^a-zA-Z0-9 ]/g, "");
Answer from Jerry on Stack OverflowUse the global flag:
var name = name.replace(/[^a-zA-Z ]/g, "");
^
If you don't want to remove numbers, add it to the class:
var name = name.replace(/[^a-zA-Z0-9 ]/g, "");
To remove the special characters, try
var name = name.replace(/[!@#$%^&*]/g, "");
regex - Efficiently removing specific characters (some punctuation) from Strings in Java? - Stack Overflow
Regex remove some characters from string
c# - Regex for removing only specific special characters from string - Stack Overflow
Using regex to remove non-alphabetical characters in a string.
Although \\p{Punct} will specify a wider range of characters than in the question, it does allow for a shorter replacement expression:
tmp = tmp.replaceAll("\\p{Punct}+", "");
Here's a late answer, just for fun.
In cases like this, I would suggest aiming for readability over speed. Of course you can be super-readable but too slow, as in this super-concise version:
private static String processWord(String x) {
return x.replaceAll("[][(){},.;!?<>%]", "");
}
This is slow because everytime you call this method, the regex will be compiled. So you can pre-compile the regex.
private static final Pattern UNDESIRABLES = Pattern.compile("[][(){},.;!?<>%]");
private static String processWord(String x) {
return UNDESIRABLES.matcher(x).replaceAll("");
}
This should be fast enough for most purposes, assuming the JVM's regex engine optimizes the character class lookup. This is the solution I would use, personally.
Now without profiling, I wouldn't know whether you could do better by making your own character (actually codepoint) lookup table:
private static final boolean[] CHARS_TO_KEEP = new boolean[];
Fill this once and then iterate, making your resulting string. I'll leave the code to you. :)
Again, I wouldn't dive into this kind of optimization. The code has become too hard to read. Is performance that much of a concern? Also remember that modern languages are JITted and after warming up they will perform better, so use a good profiler.
One thing that should be mentioned is that the example in the original question is highly non-performant because you are creating a whole bunch of temporary strings! Unless a compiler optimizes all that away, that particular solution will perform the worst.
Hi, I am trying to remove non-alphabetical characters from the following string.
let string = "Anne, I vote more cars race Rome-to-Vienna"
I can get the result I am looking for with the following...
let newString = string.toLowerCase().split('').filter(el => el !== ' ' && el !== '-' && el !== ',').join('')
However I would prefer to use a much shorter piece of code using regex. I have gone over a few regex examples and tutorials but it still kind of confused me. Does anybody know the simplest way to remove the unwanted characters from the above string?
Thanks!!!
Would /'@([a-z0-9\s]+)'/gi work for you?
"A '@computer' can sometimes be very '@expensive in price' but in other cases it can be @cheap or even free'.".replace(/'@([a-z0-9\s]+)'/gi, '$1');
You may check out https://regex101.com/ to get explanations in detail about the regexp itself.
This one should do what you need: /'@([^']*)'/g
- It looks for the
'@(corresponding part:'@) - It takes every character, that is NOT a
'and groups them (corresponding part:([^']*)) - It appends a
'to the found match for the replacement
The $1 in str.replace(regex, '$1'); references the group created in step 2
Example:
const regex = new RegExp("'@([^']*)'","g");
const testStr = "A '@computer' can sometimes be very '@expensive in price' but in other cases it can be @cheap or even free'.";
const result = testStr.replace(regex, '$1');
console.log(result);