用于匹配 N 位数字加连续数字的正则表达式
RegEx for matching N-digit plus consecutive numbers
我正在尝试在 node.js 上使用 javascript 清理输入字符串。某些输入字符串可能包含 phone 个数字(或随机数字序列),我想将其删除。例如:
输入字符串:Terrace 07541207031 RDL 18.02
清理后我希望字符串为:Terrace RDL 18.02
我想检测数字(比如大于 4 位数字)并将其删除。
This expression 可能与您所需的输入匹配。
(\s)([0-9]{4,})(\s?)
如果想匹配任意4位加数字,只需去掉左右space检查边界:
([0-9]{4,})
JavaScript演示
const regex = /(\s)([0-9]{4,})(\s?)/gm;
const str = `Terrace 07541207031 RDL 18.02
Terrace 07541 RDL 18.02
Terrace 075adf8989 RDL 18.02
Terrace 075adf898 RDL 18.02
075898 RDL 18.02
Terrace RDL 98989https://regex101.com/r/FjZqaF/1/codegen?language=javascript`;
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}`);
});
}
性能测试
此脚本 returns 输入字符串对表达式的运行时间。
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const regex = /(.*\s)([0-9]{4,})(\s.*)/gm;
const str = "Terrace 07541207031 RDL 18.02";
const subst = ``;
var match = str.replace(regex, subst);
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");
正则表达式
如果这不是您想要的表达方式,您可以 modify/change 您的表达方式 regex101.com。
正则表达式电路
您还可以在 jex.im:
中可视化您的表情
我正在尝试在 node.js 上使用 javascript 清理输入字符串。某些输入字符串可能包含 phone 个数字(或随机数字序列),我想将其删除。例如:
输入字符串:Terrace 07541207031 RDL 18.02
清理后我希望字符串为:Terrace RDL 18.02
我想检测数字(比如大于 4 位数字)并将其删除。
This expression 可能与您所需的输入匹配。
(\s)([0-9]{4,})(\s?)
如果想匹配任意4位加数字,只需去掉左右space检查边界:
([0-9]{4,})
JavaScript演示
const regex = /(\s)([0-9]{4,})(\s?)/gm;
const str = `Terrace 07541207031 RDL 18.02
Terrace 07541 RDL 18.02
Terrace 075adf8989 RDL 18.02
Terrace 075adf898 RDL 18.02
075898 RDL 18.02
Terrace RDL 98989https://regex101.com/r/FjZqaF/1/codegen?language=javascript`;
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}`);
});
}
性能测试
此脚本 returns 输入字符串对表达式的运行时间。
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const regex = /(.*\s)([0-9]{4,})(\s.*)/gm;
const str = "Terrace 07541207031 RDL 18.02";
const subst = ``;
var match = str.replace(regex, subst);
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");
正则表达式
如果这不是您想要的表达方式,您可以 modify/change 您的表达方式 regex101.com。
正则表达式电路
您还可以在 jex.im:
中可视化您的表情