如何在 javascript 中使用正则表达式仅获取不以 2 开头的数字?
How to fetch only numbers which are not starting with 2 using regex in javascript?
我有一个数字列表,如下所示:
- 200
- 302
- 301
- 201
- 205
- 500
如何使用正则表达式只获取不以 2 开头的数字?
所以输出应该如下所示:
- 302
- 301
- 500
简介
有多种方法可以完成此操作(如下所示)。
注意:我知道模式只匹配第一个字符。请阅读 完整的 答案以了解原因(以及如何在需要时匹配完整数字)。
代码
用法 部分使用每个方法的第一个正则表达式(因为它是对第一个字符的简单验证)。每个方法的第二个正则表达式允许您捕获整个数字,如果这是意图的话。
方法 1 - 正则表达式:字符集
此方法需要 39 个步骤,长度为 9 (+3) 个字符。
\b[013-9]
\b[013-9]\d*
方法 2 - 正则表达式:否定先行
此方法需要 63 个步骤,长度为 9 (+1) 个字符。
\b(?!2)\d
\b(?!2)\d*
方法 3 - 正则表达式:否定字符集
此方法需要 39 个步骤,长度为 8 (+3) 个字符。
\b[^\D2]
\b[^\D2]\d*
方法 4 - JavaScript: startsWith
startsWith("2");
方法5 - JavaScript:数组元素
假设n
是转换成字符串的数字
n[0] !== "2"
方法 6 - JavaScript:数学
假设 n
是一个正数(我们将其设为正数以进行验证)。
while(n >= 10) {
n = Math.floor(n/10);
}
if(n !== 2)
用法
var nums = [200,302,301,201,205,500];
console.log("Method 1");
var r = /\b[013-9]/g
nums.forEach(function(s){
s = s.toString();
if(s.match(r)) {
console.log(s);
}
});
console.log("\nMethod 2");
var r = /\b(?!2)\d/g
nums.forEach(function(s){
s = s.toString();
if(s.match(r)) {
console.log(s);
}
});
console.log("\nMethod 3");
var r = /\b[^\D2]/g
nums.forEach(function(s){
s = s.toString();
if(s.match(r)) {
console.log(s);
}
});
console.log("\nMethod 4");
nums.forEach(function(s){
s = s.toString();
if(!s.startsWith("2")) {
console.log(s);
}
});
console.log("\nMethod 5");
nums.forEach(function(s){
s = s.toString();
if(s[0] !== "2") {
console.log(s);
}
});
console.log("\nMethod 6");
nums.forEach(function(s){
var n = Math.abs(s);
while(n >= 10) {
n = Math.floor(n/10);
}
if(n !== 2) {
console.log(s);
}
});
结果
输入
200
302
301
201
205
500
输出
302
301
500
说明
注意:我的回答中所有的正则表达式都使用\b
(分词)来保证数字的开始。您可以根据需要替换它。例如,如果输入是多行的,您可能希望将 ^
(行断言开始)与多行标志(通常为 m
)一起使用。以下解释不包括\b
令牌的解释(因为我已经解释过了)。
方法 1 - 正则表达式:字符集
[013-9]
匹配集合中的一个字符(0、1或3-9范围内的数字)
方法 2 - 正则表达式:否定先行
(?!2)\d
负前瞻确保后面的不是 2.
方法 3 - 正则表达式:否定字符集
[^\D2]
匹配不在集合中的任何字符(任何非数字或 2)。请注意 [^\D2]
实际上是双重否定,表示 匹配任何数字,不包括 2
方法 4 - JavaScript: startsWith
Syntax
str.startsWith(searchString[, position])
Parameters
searchString
- The characters to be searched for at the start of this string.
position
Optional
- The position in this string at which to begin searching for
searchString
; defaults to 0.
Return value
true
if the given characters are found at the beginning of the
string; otherwise, false
.
Description
This method lets you determine whether or not a string begins with
another string. This method is case-sensitive.
方法5 - JavaScript:数组元素
此方法只是获取数字中的第一个数字并针对字符串 2
进行测试以查看它们是否匹配。返回 returns false
的任何内容(不以 2
开头)。
方法 6 - JavaScript:数学
此方法获取数字的绝对值 Math.abs()
and then continuously divides the number by 10 while removing decimal places (Math.floor(n/10)
) 直到数字小于 10。然后根据数字 2
检查结果以确保它不匹配 if(n !== 2)
.
我有一个数字列表,如下所示:
- 200
- 302
- 301
- 201
- 205
- 500
如何使用正则表达式只获取不以 2 开头的数字?
所以输出应该如下所示:
- 302
- 301
- 500
简介
有多种方法可以完成此操作(如下所示)。
注意:我知道模式只匹配第一个字符。请阅读 完整的 答案以了解原因(以及如何在需要时匹配完整数字)。
代码
用法 部分使用每个方法的第一个正则表达式(因为它是对第一个字符的简单验证)。每个方法的第二个正则表达式允许您捕获整个数字,如果这是意图的话。
方法 1 - 正则表达式:字符集
此方法需要 39 个步骤,长度为 9 (+3) 个字符。
\b[013-9]
\b[013-9]\d*
方法 2 - 正则表达式:否定先行
此方法需要 63 个步骤,长度为 9 (+1) 个字符。
\b(?!2)\d
\b(?!2)\d*
方法 3 - 正则表达式:否定字符集
此方法需要 39 个步骤,长度为 8 (+3) 个字符。
\b[^\D2]
\b[^\D2]\d*
方法 4 - JavaScript: startsWith
startsWith("2");
方法5 - JavaScript:数组元素
假设n
是转换成字符串的数字
n[0] !== "2"
方法 6 - JavaScript:数学
假设 n
是一个正数(我们将其设为正数以进行验证)。
while(n >= 10) {
n = Math.floor(n/10);
}
if(n !== 2)
用法
var nums = [200,302,301,201,205,500];
console.log("Method 1");
var r = /\b[013-9]/g
nums.forEach(function(s){
s = s.toString();
if(s.match(r)) {
console.log(s);
}
});
console.log("\nMethod 2");
var r = /\b(?!2)\d/g
nums.forEach(function(s){
s = s.toString();
if(s.match(r)) {
console.log(s);
}
});
console.log("\nMethod 3");
var r = /\b[^\D2]/g
nums.forEach(function(s){
s = s.toString();
if(s.match(r)) {
console.log(s);
}
});
console.log("\nMethod 4");
nums.forEach(function(s){
s = s.toString();
if(!s.startsWith("2")) {
console.log(s);
}
});
console.log("\nMethod 5");
nums.forEach(function(s){
s = s.toString();
if(s[0] !== "2") {
console.log(s);
}
});
console.log("\nMethod 6");
nums.forEach(function(s){
var n = Math.abs(s);
while(n >= 10) {
n = Math.floor(n/10);
}
if(n !== 2) {
console.log(s);
}
});
结果
输入
200
302
301
201
205
500
输出
302
301
500
说明
注意:我的回答中所有的正则表达式都使用\b
(分词)来保证数字的开始。您可以根据需要替换它。例如,如果输入是多行的,您可能希望将 ^
(行断言开始)与多行标志(通常为 m
)一起使用。以下解释不包括\b
令牌的解释(因为我已经解释过了)。
方法 1 - 正则表达式:字符集
[013-9]
匹配集合中的一个字符(0、1或3-9范围内的数字)
方法 2 - 正则表达式:否定先行
(?!2)\d
负前瞻确保后面的不是 2.
方法 3 - 正则表达式:否定字符集
[^\D2]
匹配不在集合中的任何字符(任何非数字或 2)。请注意[^\D2]
实际上是双重否定,表示 匹配任何数字,不包括 2
方法 4 - JavaScript: startsWith
Syntax
str.startsWith(searchString[, position])
Parameters
searchString
- The characters to be searched for at the start of this string.
position
Optional
- The position in this string at which to begin searching for
searchString
; defaults to 0.Return value
true
if the given characters are found at the beginning of the string; otherwise,false
.Description
This method lets you determine whether or not a string begins with another string. This method is case-sensitive.
方法5 - JavaScript:数组元素
此方法只是获取数字中的第一个数字并针对字符串 2
进行测试以查看它们是否匹配。返回 returns false
的任何内容(不以 2
开头)。
方法 6 - JavaScript:数学
此方法获取数字的绝对值 Math.abs()
and then continuously divides the number by 10 while removing decimal places (Math.floor(n/10)
) 直到数字小于 10。然后根据数字 2
检查结果以确保它不匹配 if(n !== 2)
.