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
removing all non-letter characters from a string? ((using regex))
Powershell - Remove all characters except...
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!