提取由空白包围的特定文本 [英] Extract specific text surrounded by white space

查看:35
本文介绍了提取由空白包围的特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些基本的正则表达式:

I have written some basic regex:

/[0123BCDER]/g

我想从下面几行中提取粗体数字.我写的正则表达式提取了字符,但是我只想提取被空格包围的字符.任何帮助将不胜感激.

I would like to extract the bold numbers from the below lines. The regex i have written extracts the character however i would only like to extract the character if it is surrounded by white space. Any help would be much appreciated.

 721658A220421EE5867            AMBER YUR DE STE 30367887462580                  **1**                                                        00355132

 172638A220421ER3028            NIKITA YUAN         318058763400580                  **1**                                                        00355133

 982658A230421MC1234            SEAN D W MC100050420965155230421            **3**                  14032887609303                        00355134

请注意,字符或数字将始终单独存在.

Please note the character or digit will always be by itself.

推荐答案

您正在寻找这样的东西:/\s\d\s/g.

You are loking for something like this: /\s\d\s/g.

  • \s - 匹配空格,
  • \d - 匹配任何数字,
  • /g - 匹配所有出现.
  • \s - match whitespace,
  • \d - match any digit,
  • /g - match all occurrences.

您也可以将 \d 替换为例如[0123BCDER](您的示例)或 [0-9A-Za-z](全部为字母数字).

You can also replace \d with e.g. [0123BCDER] (your example) or [0-9A-Za-z] (all alphanumberic).

const input = `721658A220421EE5867            AMBER YUR DE STE 30367887462580                  1                                                       00355132

 172638A220421ER3028            NIKITA YUAN         318058763400580                  1                                                        00355133      _

 982658A230421MC1234            SEAN D W MC100050420965155230421            3                  14032887609303                        00355134
`

// with whitespaces
const res = input.match(/\s\d\s/g)
console.log(res)

// alphanumeric 
const res2 = input.match(/\s[A-Za-z0-9]\s/g)
console.log(res2)

这篇关于提取由空白包围的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆