[英]Remove all characters from string javascript
$('.hourfield').focusout(function() {
var h;
var m;
var timeStr = "";
var time = "";
var newFormat = "";
timeStr = $(this).val();
//Here I would like to remove all characters which isn't numbers
timeStr = timeStr.replace("/[^0-9\.]+/g","");
if(timeStr > 0) {
h = timeStr.substr(0,2);
m = timeStr.substr(2,2);
newFormat = h+':'+m;
//Add new values
$(this).val(newFormat);
}
});
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 我对replace()规则做了一些小的更改,只是删除了主要目的“点”
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! 所以现在工作正常,谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.