[JavaScript] -Regex代码输入文本字段不允许从0开始,但允许0,不允许字符 , -



请参阅,我正在寻找一个正则代码,其中文本字段应接受这些

  1. 只有正数
  2. 可以允许0
  3. 不应允许 , - 。

它不应匹配:0345,7。,7 , 7,.7,-7,7-,..7

它一定不能接受: 1. 2. - 3.

nb:我不想要按键功能,我正在寻找正则

使用以下: ^(0|[1-9][0-9]*)$

演示:https://regex101.com/r/natdio/1。

这将有任何帮助

$re = '/([1]d+)/';
$str = '0123';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);

现在适用于JavaScript等效

const regex = /([1]d+)/g;
const str = `0123`;
let m;
while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

最新更新