Javascript 十进制为0.1到1的正则表达式范围
我正在使用以下正则表达式:Javascript 十进制为0.1到1的正则表达式范围,javascript,regex,range,Javascript,Regex,Range,我正在使用以下正则表达式: /^(?:1|0?\.\d)$/ 我需要的范围在0.1和1之间,每个数字在点之前和之后。例如: 0.1 valid 1 valid 0.111 not valid 011.2 not valid 有效数字为[0.1-0.9]和1对于给定的测试数据,以下工作: (^|[^0-9])(0\.[1-9]|1\.0|1)($|[^0-9]) 对于给定的测试数据,以下操作有效: (^|[^0-9])(0\.[1-9]|1\.0|1)($|[^0-9]) 鲍勃的答案是正
/^(?:1|0?\.\d)$/
我需要的范围在0.1和1之间,每个数字在点之前和之后。例如:
0.1 valid
1 valid
0.111 not valid
011.2 not valid
有效数字为[0.1-0.9]和1对于给定的测试数据,以下工作:
(^|[^0-9])(0\.[1-9]|1\.0|1)($|[^0-9])
对于给定的测试数据,以下操作有效:
(^|[^0-9])(0\.[1-9]|1\.0|1)($|[^0-9])
鲍勃的答案是正确的,但是,它可以做一些调整
- 因此它与
1.|不匹配。1
- 因此,返回的捕获不包括
\n
- 因此,返回的匹配项是一个一维数组
(?<=^|[^[0-9]\.])(0\.[1-9]|1)(?=$|[^[0-9]\.])
(?<= : Start of positive look behind
^ : Matches start of string
| : OR operator
[^0-9\.] : Matches anything but 0-9 or literal "."
) : End of look behind group
( : Start of capture group
0\.[1-9] : Matches 0.1 -> 0.9
| : OR operator
1 : Matches 1 on it's own
) : End of capture group
(?= : Start of positive look ahead
$ : Matches end of string
| : OR operator
[^0-9\.] : Matches anything BUT 0-9 or literal "."
) : End of look ahead
标志
您还需要实现几个正则表达式标志:
g : Global
m : Multiline mode
JS示例
var data = `
0.1 : Match
1 : Match
0.111 : No-match
011.2 : No-match
1.2 : No-match
0.2 : Match
0 : No-match
0.1 : Match
.1 : No-match
1.0 : Match
`;
var re = /(?<=^|[^\d\.])(0\.[1-9]|1)(?=$|[^\d\.])/gm;
console.log(data.match(re)); // Output: ["0.1", "1", "0.2", "0.1"]
var数据=`
0.1:匹配
1:比赛
0.111:不匹配
011.2:不匹配
1.2:没有对手
0.2:匹配
0:没有对手
0.1:匹配
1:没有对手
1.0:比赛
`;
var re=/(?Bob的答案是正确的,但是,它可以通过一些调整来实现
- 这样它就不匹配
1.|.1
- 因此,返回的捕获不包括
\n
- 因此,返回的匹配项是一个一维数组
RegEx
(?<=^|[^[0-9]\.])(0\.[1-9]|1)(?=$|[^[0-9]\.])
(?<= : Start of positive look behind
^ : Matches start of string
| : OR operator
[^0-9\.] : Matches anything but 0-9 or literal "."
) : End of look behind group
( : Start of capture group
0\.[1-9] : Matches 0.1 -> 0.9
| : OR operator
1 : Matches 1 on it's own
) : End of capture group
(?= : Start of positive look ahead
$ : Matches end of string
| : OR operator
[^0-9\.] : Matches anything BUT 0-9 or literal "."
) : End of look ahead
标志
您还需要实现几个正则表达式标志:
g : Global
m : Multiline mode
JS示例
var data = `
0.1 : Match
1 : Match
0.111 : No-match
011.2 : No-match
1.2 : No-match
0.2 : Match
0 : No-match
0.1 : Match
.1 : No-match
1.0 : Match
`;
var re = /(?<=^|[^\d\.])(0\.[1-9]|1)(?=$|[^\d\.])/gm;
console.log(data.match(re)); // Output: ["0.1", "1", "0.2", "0.1"]
var数据=`
0.1:匹配
1:比赛
0.111:不匹配
011.2:不匹配
1.2:没有对手
0.2:匹配
0:没有对手
0.1:匹配
1:没有对手
1.0:比赛
`;
var re=/(?使用
看
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
0? '0' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
使用
看
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
0? '0' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
.1
有效吗?关于0.2
如何?是的,0.2是有效的\d
与[1-9]
/^1$^0(\.\d{1})$/
(匹配0、1和0.x)或/^1$^0.\d{1}$/
(匹配1和0.x)@zana10您从未实际说明您当前正则表达式的问题/错误。是否.1
有效?关于0.2
?是的,0.2是有效的\d
与[1-9]
/^1$^0(\.\d{1})?$/
(匹配0、1和0.x)或/^1$^0.\d{1}(匹配1和0.x)@zana10您从未实际说明您的问题是什么/当前正则表达式有什么问题。不要轻描淡写您的答案。似乎^0\[1-9]$^1$^1.0$也涵盖了所有这些模式。@Popeye您是对的,如果要搜索的字符串只包含预期的数据?例如var input=“1.0”;
但是,如果它包含任何其他内容,那么它就不匹配,例如var-input=“1.0”
因为空格而不匹配。它也不会在一个字符串中匹配多个,例如var-input=“0.1\n1.0”
?感谢您的解释。我正在尝试将此修改为不接受1。0@zana10我对上面的代码做了一些修改…它现在应该匹配0.1-0.9&1
。上面的正则表达式现在也使用积极的向前看和向后看,因为我注意到它捕获了输出中的空白!@zana10所以…在你的评论中,你需要说明ify^
和$
;因此,我现在假设字符串周围不应该有任何其他字符。在这种情况下,您可以在我的答案中使用正则表达式的中间部分,并用^
和$
环绕,例如:^(0\[1-9]| 1)$
不要低估您的答案。看起来像^0\.[1-9]$|^1$|^1.0$也涵盖了所有这些模式。@大力水手您说得对,如果要搜索的字符串仅包含预期的数据,则会这样做?例如var input=“1.0”
但是,如果它包含任何其他内容,则它不匹配,例如var input=“1.0”
因空格而不匹配。它也不匹配一个字符串中的多个字符串,例如var input=“0.1\n1.0”
?感谢您的解释。我正在尝试将此修改为不接受1。0@zana10我对上面的代码做了一些修改…它现在应该匹配0.1-0.9&1
。上面的正则表达式现在也使用积极的向前看和向后看,因为我注意到它捕获了输出中的空白!@zana10所以…在你的评论中,你需要说明ify^
和$
;因此我现在假设字符串周围不应该有任何其他字符。在这种情况下,您可以在我的答案中使用正则表达式的中间部分,并用^
和$
环绕,比如:^(0\[1-9]| 1)$