admin管理员组文章数量:1026989
I have a string. I need to parse it, replacing any chars (except numbers (0
to 9
),,
and .
.
How can I do it with javascript?
Tried with :
string.replace(/[a-zA-Z]*/, "");
but seems it doesnt works. Also, I need ANY chars, not only a-Z (also ?, /, white space, and so on, expect, as said, , and .
I have a string. I need to parse it, replacing any chars (except numbers (0
to 9
),,
and .
.
How can I do it with javascript?
Tried with :
string.replace(/[a-zA-Z]*/, "");
but seems it doesnt works. Also, I need ANY chars, not only a-Z (also ?, /, white space, and so on, expect, as said, , and .
Share Improve this question edited Oct 17, 2017 at 22:31 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked Feb 13, 2012 at 14:02 markzzzmarkzzz 47.9k126 gold badges316 silver badges534 bronze badges 1 |4 Answers
Reset to default 13Use a negated character class
[^0-9,.]
and put in any character you don't want to be replaced, it will match all the others.
See it here on Regexr
You can optimize it by adding a quantifier +
, so that it replaces a sequence of those characters at once and not each by each.
string.replace(/[^0-9,.]+/g, "");
As Felix Kling pointed out, its important to use the global option g
to match on all occurrences of the pattern.
See it here on Regexr
string.replace(/[^\d,.]+/g, "");
should do.
[^\d.,]
means "any character except digit, comma or period", a negated character class.- Use
+
instead of*
or you'll have lots of empty matches replaced with empty strings. - Use
/g
so all instances are replaced.
Try to replace all chars except digits and .
,
and do the replace globally:
string.replace(/[^0-9\,\.]+/g, "");
Try This Below code is prevent alpha key
onKeypress="return Custom(event,'[0-9]');"
function Custom(e, cPattern) {
var key = [e.keyCode || e.which];
var pattern = new String(cPattern);
var keychar = String.fromCharCode([e.keyCode || e.which]);
var pattern1 = new RegExp(pattern);
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
return true;
}
else if (pattern1.test(keychar)) {
return true;
}
else {
e.preventDefault ? e.preventDefault() : e.returnValue = false;
}
}
I have a string. I need to parse it, replacing any chars (except numbers (0
to 9
),,
and .
.
How can I do it with javascript?
Tried with :
string.replace(/[a-zA-Z]*/, "");
but seems it doesnt works. Also, I need ANY chars, not only a-Z (also ?, /, white space, and so on, expect, as said, , and .
I have a string. I need to parse it, replacing any chars (except numbers (0
to 9
),,
and .
.
How can I do it with javascript?
Tried with :
string.replace(/[a-zA-Z]*/, "");
but seems it doesnt works. Also, I need ANY chars, not only a-Z (also ?, /, white space, and so on, expect, as said, , and .
Share Improve this question edited Oct 17, 2017 at 22:31 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked Feb 13, 2012 at 14:02 markzzzmarkzzz 47.9k126 gold badges316 silver badges534 bronze badges 1-
Create a negated character class: regular-expressions.info/charclass.html. Don't forget the
g
lobal modifier. – Felix Kling Commented Feb 13, 2012 at 14:04
4 Answers
Reset to default 13Use a negated character class
[^0-9,.]
and put in any character you don't want to be replaced, it will match all the others.
See it here on Regexr
You can optimize it by adding a quantifier +
, so that it replaces a sequence of those characters at once and not each by each.
string.replace(/[^0-9,.]+/g, "");
As Felix Kling pointed out, its important to use the global option g
to match on all occurrences of the pattern.
See it here on Regexr
string.replace(/[^\d,.]+/g, "");
should do.
[^\d.,]
means "any character except digit, comma or period", a negated character class.- Use
+
instead of*
or you'll have lots of empty matches replaced with empty strings. - Use
/g
so all instances are replaced.
Try to replace all chars except digits and .
,
and do the replace globally:
string.replace(/[^0-9\,\.]+/g, "");
Try This Below code is prevent alpha key
onKeypress="return Custom(event,'[0-9]');"
function Custom(e, cPattern) {
var key = [e.keyCode || e.which];
var pattern = new String(cPattern);
var keychar = String.fromCharCode([e.keyCode || e.which]);
var pattern1 = new RegExp(pattern);
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
return true;
}
else if (pattern1.test(keychar)) {
return true;
}
else {
e.preventDefault ? e.preventDefault() : e.returnValue = false;
}
}
本文标签: javascriptregex that replace any chars (except number)Stack Overflow
版权声明:本文标题:javascript - regex that replace any chars (except number, .) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1738754533a1597043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
g
lobal modifier. – Felix Kling Commented Feb 13, 2012 at 14:04