简体   繁体   English

如何匹配和提取字符串匹配正则表达式

[英]How to match and extract string matching regular expression

This is main string:这是主字符串:

MR HI Government He PIHe9 Hanumana Ji 3-� fafer/ DOB : 01/01/1959 989 / Male 2094 7051 9541 ������ - ��� ����� �� 3�1���

I want to match and extract 2094 7051 9541 using regular expression我想使用正则表达式匹配并提取2094 7051 9541

and regex pattern to find is:要查找的正则表达式模式是:

^[2-9]{1}[0-9]{-3}\\s[0-9]{4}\\s[0-9]{4}$

I want to use javascript to match and extract string.我想使用 javascript 来匹配和提取字符串。 But not able to find right syntax to it.但无法找到正确的语法。

Any help would be appreciated.任何帮助,将不胜感激。

Thanks, PD谢谢,警察局

You can use您可以使用

const regex = /\b[2-9]\d{3}\s\d{4}\s\d{4}\b/;

See the regex demo .请参阅正则表达式演示 Note the use of a regex literal that helps avoid double escaping backslashes.请注意使用有助于避免双重转义反斜杠的正则表达式文字。

Details细节

  • \b - a word boundary \b - 单词边界
  • [2-9] - a digit from 2 to 9 [2-9] - 从29的数字
  • \d{3} - three digits \d{3} - 三位数
  • \s - a wjitespace \s - 一个 wjitespace
  • \d{4} - four digits \d{4} - 四位数字
  • \s - a whitespace \s - 一个空格
  • \d{4} - four digits \d{4} - 四位数字
  • \b - a word boundary. \b - 单词边界。

The word boundaries avoid matching the number as part of another number/word.单词边界避免将数字匹配为另一个数字/单词的一部分。

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

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