gpt4 book ai didi

JavaScript;正则表达式并排只有 5 位数字的字符串

转载 作者:行者123 更新时间:2023-11-30 15:01:59 25 4
gpt4 key购买 nike

我有这个代码:

<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function() {
var src = document.getElementById("one"),
dst = document.getElementById("two");
src.addEventListener('input', function() {
dst.value = src.value.replace(/\D/g,'');
});
};
</script>
</head>
<body>

<input type="text" id="one" name="one">
<input type="text" id="two" name="two">

</body>
</html>

这将从 one 中删除非数字的所有内容并插入到 two 中。

但我只想并排保留一串五位数字。

意味着,如果 one 是:

测试1测试1234测试;无输出

测试1 12345测试;输出 12345

test 123456 test 12 test;无输出

12394测试;输出 12394

84725;输出 84725

有谁知道如何更改正则表达式来达到这个目的吗?

最佳答案

您可以使用\d 来匹配数字,并使用{5} 指定您选择的长度。然后,单词边界 (\b) 确保没有其他数字与您匹配:

/\b\d{5}\b/g

关于JavaScript;正则表达式并排只有 5 位数字的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com