JavaScript正则表达式 - 匹配一系列十六进制数字 [英] JavaScript regular expressions - match a series of hexadecimal numbers

查看:259
本文介绍了JavaScript正则表达式 - 匹配一系列十六进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候JavaScript和正则表达式大师,

Greetings JavaScript and regular expression gurus,

我想返回输入字符串中的所有匹配项,这些字符串是6位十六进制数字,其间有任意数量的空格。例如,333333 e1e1e1 f4f435应该返回一个数组:

I want to return all matches in an input string that are 6-digit hexadecimal numbers with any amount of white space in between. For example, "333333 e1e1e1 f4f435" should return an array:

array[0] = 333333  
array[1] = e1e1e1  
array[2] = f4f435

这是我所拥有的,但它是不是很正确 - 我不清楚如何获得可选的空格,我只得到一场比赛。

Here is what I have, but it isn't quite right-- I'm not clear how to get the optional white space in there, and I'm only getting one match.


colorValuesArray = colorValues.match(/ [0-9A-Fa-f] {6} /);

colorValuesArray = colorValues.match(/[0-9A-Fa-f]{6}/);

谢谢你的帮助,

-NorthK

推荐答案

使用 g 标志全球匹配:

/[0-9A-Fa-f]{6}/g

另一个好的增强功能是添加字边界:

Another good enhancement would be adding word boundaries:

/\b[0-9A-Fa-f]{6}\b/g

如果您愿意,还可以为不区分大小写的匹配设置 i 标志:

If you like you could also set the i flag for case insensitive matching:

/\b[0-9A-F]{6}\b/gi

这篇关于JavaScript正则表达式 - 匹配一系列十六进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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