简体   繁体   English

Javascript正则表达式选择未连接到字母的数字

[英]Javascript regex selecting numbers that are not connected to letters

This is my current regex: 这是我当前的正则表达式:

var re = '/\d+\s+[\d+a-z]+/gi'; 

I'm trying to get my regex to select numbers before if there are there own and then any abc characters and numbers after a whitespace 我正在尝试让我的正则表达式在出现空格之前先选择数字,然后在空格后选择任何abc字符和数字

This is my test string 这是我的测试字符串

part1 6

22 part2

part3 8

At the moment its selecting the following 目前其选择以下内容

1 : 6

22 part2

3 8

But I just want to be selecting this 但我只想选择这个

22 part2

So i'm only wanting to select this if the number is on its own. 所以我只想选择这个,如果数字是单独的。

Any help thanks ? 任何帮助谢谢吗?

The regex is /^\\d+\\s[a-zA-Z0-9]+$/ 正则表达式为/^\\d+\\s[a-zA-Z0-9]+$/

  • ^ start ^开始
  • \\d+\\s 1 or more digits and 1 space \\d+\\s 1个或多个数字和1个空格
  • [a-zA-Z0-9]+ the alpha numeric combination [a-zA-Z0-9]+字母数字组合
  • $ end $结束

 var arr=["part1 6","22 part2","part3 8"]; var re=/^\\d+\\s[a-zA-Z0-9]+$/; arr.forEach(x => console.log(x,re.test(x))); 

你可以试试这个正则表达式

regex : /^\d+\s[a-zA-Z0-9]+\s\d+$;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM