- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试格式化字段,以便将输入到 input
中的整数预格式化为特定格式。
目前我有包含的 JQuery,它将输入的字符串格式化为 xx.xx.xx.xx,我如何强制格式为 x.xx.xx.xxx 并添加 8 个整数的限制?
$('.versionNum').keyup(function() {
var foo = $(this).val().split(".").join(""); // remove periods
if (foo.length > 0) {
foo = foo.match(new RegExp('.{1,2}', 'g')).join(".");
}
$(this).val(foo);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="versionNum" />
最佳答案
我不认为使用正则表达式构造输出是最易读的选择 - 它会有点复杂且难以阅读。相反,一旦您有了一串数字,切片
您需要的每个部分,然后按 bool 值过滤(以删除空字符串)并按点连接:
$('.versionNum').keyup(function() {
const nums = $(this).val().replace(/\D/g, '');
const n0 = nums[0];
const n1 = nums.slice(1, 3);
const n2 = nums.slice(3, 5);
const n3 = nums.slice(5, 8);
$(this).val(
[n0, n1, n2, n3].filter(Boolean).join('.')
);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="versionNum" />
正则表达式版本:
$('.versionNum').keyup(function() {
const numStr = $(this).val().replace(/\D/g, '');
const [, ...arr] = numStr.match(/(\d?)(\d{0,2})(\d{0,2})(\d{0,3})/);
$(this).val(
arr.filter(Boolean).join('.')
);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="versionNum" />
关于javascript - 用句点格式化输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076042/
我有以下字符串: 00:02:44.372 我想使用正则表达式来提取句号/句点。 我尝试过使用: [0-9][0-9]:[0-9][0-9]:[0-9][0-9] 选择持续时间,但我从这里去哪里?由于
我想知道如果我有这样的文件名,我如何去掉文件名中的句点: Test....1.txt 看起来像 Test 1.txt?我不希望像 1.0.1 Test.txt 这样的文件被触及。只有具有连续句点的文件
试图找到一个好的正则表达式来检测java中的句子结尾。主要问题是,如果有数字然后句点,它会将其检测为句子结尾(请参阅演示链接)。但就我而言,我希望它不将其识别为句子结尾,尽管在某些情况下可能是这样。我
我开始学习 Backbone,但 UnderscoreJS 似乎给我带来了一些问题。我收到“意外 token ”。我不确定这意味着什么。我的 JSON 看起来不错,所以我不认为这是一个原因。 var
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
在日期时间选择器控件中,用户必须按.(句点)才能移动到不同的字段(如日、月、年) 我想通过允许用户输入日期而不输入.(句点)来覆盖此行为 有没有什么可以在 C# winforms 中以编程方式实现这一
对于那些不知道 GMAIL 的人,您可以根据原始电子邮件创建无限数量的电子邮件。如果您的电子邮件是“mygmail@gmail.com”,那么这可以通过两种方式实现: 1) 在您的 gmail 末尾的
你们中的一位 CSS 专家可以向我解释一下这个指示符(如果您愿意这样调用它的话)吗?我了解内容,只是不了解 a.button.gold。两个点? a.button.gold{ background-c
很抱歉描述模糊,找不到更好的方式来说明它。 我从 F# 开始,和许多其他人一样,我将解决的欧拉问题转换为 F#。我喜欢使用测试来运行我的代码,也喜欢 FsUnit 风格。在给定示例的帮助下,我这样做了
这是一个奇怪的。通常,当我从powershell执行外部命令时,我使用&运算符,如下所示: & somecommand.exe -p somearguments 但是,今天我遇到了像这样使用的.运算符
我想允许 . 和 a-zA-Z 字母以及 _ 和 -,我的 .. 有一些问题 有什么想法吗? 最佳答案 [A-Za-z_.-] 是一个字符类,包含您提到的所有字符。在字符类中,没有必要对 . 进行转义
所以我知道我需要使用前瞻和后视的东西,但我开始失去理智。 因此,您能否提供一个示例,并解释其含义。 我需要匹配以下序列中的点,但不匹配各个字符之间的点。 this.is.a.sentence.e.g.
在 Python 中,我需要从列表中删除几乎所有标点符号,但保留句点和逗号。我应该创建一个函数来执行此操作还是创建一个变量?基本上我想删除除字母(我已经将大写字母转换为小写)以及句点和逗号(可能还有撇
我只是想选择这些特定“边”的 ID。我已经做了一百万次了,但 jQuery 只是不想与我合作。 $( 'input[name="fd-border"]' ).change(function() {
我正在尝试制作一个正则表达式,它允许使用大小字母 + 数字创建字符串 - a-zA-z0-9 以及字符:。 -_ 如何制作这样的正则表达式? 最佳答案 以下正则表达式应该是您要查找的内容(下面的解释)
我想检查以 number 开头的字符串, period(.)和 space .为此,我使用了 regex但这并没有给出正确的答案。 输入: 1. this a paragaraph1
我在创建正则表达式验证器时遇到问题,该验证器会检查以确保输入仅包含大写或小写字母字符、空格、句点、下划线和破折号。无法通过搜索在线找到此示例。例如: 这些没问题: Dr. Marshall sam s
以下网址有效吗? http://www.example.com/module.php/lib/lib.php 根据https://www.rfc-editor.org/rfc/rfc1738部分 UR
我正在尝试发送一个采用 JSON 对象的 HttpRequest: { "some.setting.withperiods":"myvalue" } 我一直在为我的其他请求创建匿名对象,但我不能
由于大量的搜索,我已经走到了这一步,但我坚持只格式化链接而不格式化文本的方法。我希望链接在文本中包含数字,但没有空格、括号、句号或减号,并保留文本不变。该链接应以 555 123-1234 为例。
我是一名优秀的程序员,十分优秀!