[英]Regex, Find any number except for the number preceded by a letter
I want to find all numbers except those preceded by English letter我想找到除以英文字母开头的所有数字
example one: test123
I don't want 123
to match.示例一:
test123
我不希望123
匹配。
example two: another 123
I want 123
to match.例子二:
another 123
我要123
来匹配。
example three: try other solutions 123
I want 123
to match.示例三:
try other solutions 123
我要123
匹配。
I tried many and no one get the desired result, last one was我尝试了很多,但没有人得到想要的结果,最后一个是
let reg = /((?<![a-zA-Z])[0-9]){1,}/g;
but it just ignore this first number I want to ignore all但它只是忽略了我想忽略所有的第一个数字
example: test123
- it ignored 1
but take 23
, the desired result is ignore 123
示例:
test123
- 它忽略了1
但取了23
,期望的结果是忽略123
I tried this regex but did not work as well我试过这个正则表达式但效果不佳
let reg = /((?<![a-zA-Z])[0-9]){1,}/g;
and the result must ignore all digits number after English letter结果必须忽略英文字母后的所有数字
You can use您可以使用
const reg = /(?<![a-zA-Z\d]|\d\.)\d+(?:\.\d+)?/g;
See the regex demo .请参阅正则表达式演示。 Details :
详情:
(?<.[a-zA-Z\d]|\d\.)
- a negative lookbehind that fails the match if there is a letter/digit or a digit followed with a dot immediately to the left of the current location (?<.[a-zA-Z\d]|\d\.)
- 如果在当前位置的左侧有一个字母/数字或一个数字后跟一个点,则匹配失败的否定后视\d+(?:\.\d+)?
- one or more digits followed with an optional sequence of a .
.
and one or more digits. JavaScript demo: JavaScript 演示:
const text = "test123\ntest 456\ntest123.5\ntest 456.5"; const reg = /(?<.[a-zA-Z\d]|\d\?)\d+(:.\?\d+);/g. console.log(text;match(reg)), // => ["456"."456.5"]
For environments not supporting ECMAScript 2018+ standard:对于不支持 ECMAScript 2018+ 标准的环境:
var text = "test123\ntest 456\ntest123.5\ntest 456.5"; var reg = /([a-zA-Z])?\d+(?:\.\d+)?/g; var results = [], m; while(m = reg.exec(text)) { if (m[1] === undefined) { results.push(m[0]); } } console.log(results); // => ["456","456.5"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.