Regular expressions [xkcd] (I feel like him ;)):
s = s.replace(/[\[\]&]+/g, '');
Reference:
- MDN -
string.replace - MDN - Regular Expressions
- http://www.regular-expressions.info/
Side note:
JavaScript's replace function only replaces the first occurrence of a character. So even your code would not have replaced all the characters, only the first one of each. If you want to replace all occurrences, you have to use a regular expression with the global modifier.
Regular expressions [xkcd] (I feel like him ;)):
s = s.replace(/[\[\]&]+/g, '');
Reference:
- MDN -
string.replace - MDN - Regular Expressions
- http://www.regular-expressions.info/
Side note:
JavaScript's replace function only replaces the first occurrence of a character. So even your code would not have replaced all the characters, only the first one of each. If you want to replace all occurrences, you have to use a regular expression with the global modifier.
Today, in 2021 you can use the replaceAll function:
let str = "Hello. My name is John."
let newStr = str.replaceAll('.', '')
console.log(newStr) // result -> Hello My name is John
let nextStr = str.replaceAll('.', '&')
console.log(nextStr) // result -> Hello& My name is John&
javascript - How to remove all characters from a string - Stack Overflow
jquery - Remove all characters from string javascript - Stack Overflow
regex - Javascript remove all characters from string which are not numbers, letters and whitespace - Stack Overflow
removing all non-letter characters from a string? ((using regex))
You can use the replace method:
'Hey! The #123 sure is fun!'.replace(/[^A-Za-z]+/g, '');
>>> "HeyThesureisfun"
If you wanted to keep spaces:
'Hey! The #123 sure is fun!'.replace(/[^A-Za-z\s]+/g, '');
>>> "Hey The sure is fun"
The regex /[^a-z\s]/gi is basically saying to match anything not the letter a-z or a space (\s), while doing this globally (the g flag) and ignoring the case of the string (the i flag).
RegEx instance properties used g , i
global : Whether to test the regular expression against all possible matches in a string, or only against the first.
ignoreCase : Whether to ignore case while attempting a match in a string.
RegEx special characters used [a-z] , +
[^xyz] : A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets. You can specify a range of characters by using a hyphen.
For example,
[abcd]is the same as[a-d]. They match the 'b' in "brisket" and the 'c' in "chop".+ : Matches the preceding item 1 or more times. Equivalent to {1,}.
JavaScript string replace method syntax
str.replace(regexp|substr, newSubStr|function[, Non-standard flags]);
The non-standard flags g & i can be passed in the replace syntax or built into the regex.
examples:
var re = /[^a-z]+/gi; var str = "this is a string"; var newstr = str.replace(re, ""); print(newstr);
var str = "this is a string"; var newstr = str.replace(/[^a-z]+/, "", "gi"); print(newstr);
To match whitespace characters as well \s would be added to the regex [^a-z\s]+.
JavaScript Reference
You've specified a string to replace by enclosing the regex in quotes. Remove the quotes to specify a Regex.
timeStr = timeStr.replace(/[^0-9\.]+/g,"");
$('.hourfield').focusout(function() {
var h;
var m;
var timeStr = "";
var CleanTimeStr = "";
var newFormat = "";
timeStr = $(this).val();
I did some minor changes to the replace() rule and just removed the "dots" which was the main purpose
CleanTimeStr = timeStr.replace(/[.]+/g,"");
if(CleanTimeStr > 0) {
h = CleanTimeStr.substr(0,2);
m = CleanTimeStr.substr(2,2);
newFormat = h+':'+m;
//Add new values
$(this).val(newFormat);
}
});
So now it works fine, thanks!
As @chris85 said, you can use the regular expression [^0-9a-z-A-Z] to replace all the characters that are not letters, numbers, or whitespace.
Here is a function that will do what you want:
function clean(str) {
return str.replace(/[^0-9a-z-A-Z ]/g, "").replace(/ +/, " ")
}
The second replace call is needed to remove the runs of extra whitespace that are made from removing a character that is between spaces.
Use replace
string.replace(/[^A-Z\d\s]/gi, '')
Note the two flags at the end of the regex
g - stands for global, and means that every such instance of the regular expression will be found
i - stands for case insensitive. That means it will match both lower case and uppercase characters
Playing with your string, it returns this output
"Players got to play justsaying"
To transform two or more whitespace characters into a single whitespace, you could hain another replace method to the existing ones.
string.replace(/[^A-Z\d\s]/gi, '').replace(/\s+/g, ' ')
The key here is the + character, which finds one or more.
It's probably possible to do it more efficiently, but I'm an amateur in Regex.